I have the following queries –
SELECT COUNT(capture_id) as count_captures
FROM captures
WHERE user_id = 9
…returns 5
SELECT COUNT(id) as count_items
FROM items
WHERE creator_user_id = 9
…returns 22
I tried the following query –
SELECT COUNT(capture_id) as count_captures,
COUNT(items.id) as count_items
FROM captures
LEFT JOIN items ON captures.user_id = items.creator_user_id
WHERE user_id = 9
…but it returns two columns both with 110 as the value. I would want 5 in one column and 22 in the other. What am I doing wrong?
My knee-jerk is a subquery:
I’m not really sure what you can do to avoid this. You’re seeing expected (and generally desired behavior).
Of course, if you know that the ID’s in both won’t repeat themselves, you can use distinct: