Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Ecto] SELECT bookshelf.id AS bookshelf_id, COUNT(book.id) AS books_count FROM bookshelf JOIN book ON bookshelf.id = book.bookshelf_id GROUP BY bookshelf.id
from(bookshelf in Bookshelf,
join: book in Book, on bookshelf.id == book.bookshelf_id,
group_by: bookshelf.id,
select: %{bookshelf_id: bookshelf.id, books_count: count(book.id)}
) |> Repo.all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment