Skip to content

Instantly share code, notes, and snippets.

@ytnobody
Created December 2, 2019 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytnobody/abac5a34fa533e9185208e82c0dfb039 to your computer and use it in GitHub Desktop.
Save ytnobody/abac5a34fa533e9185208e82c0dfb039 to your computer and use it in GitHub Desktop.
[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