I would just like a join to return 0 rather than dropping rows. Currently, if I try to join the two tables and count the number of items for each subscription, it will work fine:
SELECT S.id, count(I.id) FROM "Subscription" S
LEFT JOIN "Item" I on I.id = S.item_id
WHERE S.status = 1
GROUP BY S.id
returns:
id | count(I.id)
1 4
2 3
3 2
whereas:
SELECT S.id, count(I.id) FROM "Subscription" S
LEFT JOIN "Item" I on I.id = S.item_id
WHERE S.status = 1
AND I.status IN (1,2,3)
GROUP BY S.id
returns:
id | count(I.id)
1 3
I realise this is probably how Postgres should work, but Im used to using MySQL.
I've tried aggregate functions to produce the desired effect as well as conditional statements and right, full and outer joins but cannot seem to get it working the way I want.
What if I want to return all of the users with status 1 but show 0 items if they have no items with status 1,2 or 3?
From this documentation it seems like the code Im using should work.
The desired result is:
id | count(I.id)
1 3
2 0
3 0