Count relations
Sometimes you may want to return a count of relations (for example, a user’s post count). To accomplish this, you can do as the example below.
Having the query below in the file select-users-with-post-count.sql
:
SELECT u.name, count(p.id) as postCountFROM users uLEFT JOIN posts p on p.fk_user = u.idGROUP BY u.idORDER BY count(p.id) desc
TypeSQL will gererate the function selectUsersWithPostCount
:
const usersWithPostCount = await selectUsersWithPostCount(conn);console.table(usersWithPostCount);
Result:
(index) | name | postCount |
---|---|---|
0 | user2 | 3 |
1 | user1 | 2 |
2 | user3 | 1 |
3 | user4 | 0 |