Skip to content
Siim Kinks edited this page Mar 21, 2017 · 2 revisions

For counting query result set rows there is a count() method on the query builder API. By defining this method, during compilation phase, count(*) will be set as the only selected column if needed. Executing methods produce long type result indicating how many rows a query would return.

SQL SqliteMagic
   SELECT count(*)
     FROM BOOK
LEFT JOIN AUTHOR
          ON AUTHOR.ID = BOOK.AUTHOR
    WHERE BOOK.TITLE LIKE '%Foo%' 
          AND BOOK.PAGES > 200
 GROUP BY AUTHOR.FIRST_NAME,
          AUTHOR.LAST_NAME
   HAVING COUNT(*) > 42;
import static com.siimkinks.sqlitemagic.AuthorTable.AUTHOR;
import static com.siimkinks.sqlitemagic.BookTable.BOOK;
import static com.siimkinks.sqlitemagc.Select.count;

long nrOfBooks = Select
    .from(BOOK)
    .leftJoin(AUTHOR
            .on(AUTHOR.ID.is(BOOK.AUTHOR)))
    .where(BOOK.TITLE.like("%Foo%")
            .and(BOOK.PAGES.greaterThan(200)))
    .groupBy(AUTHOR.FIRST_NAME,
             AUTHOR.LAST_NAME)
    .having(count().greaterThan(4L))
    .count()
    .execute();

See Next

Clone this wiki locally