tags:

views:

77

answers:

2

This query:

SELECT staff.staff_id, COUNT(references_table.staff_id) 
FROM staff 
LEFT JOIN references_table USING (staff_id)

returns this:

staff_id    COUNT(references_table.staff_id)
1            2

How can I get it to return 0 as the count for staff_ids that don't have any references?

+1  A: 

Try a left outer join.

Chris Lively
'OUTER' in 'LEFT OUTER JOIN' is redundant in MySQL syntax
John Douthat
I was under the impression that LEFT JOIN and LEFT OUTER JOIN were synonymous.
Matthew
My bad. I didn't see the MySql tag.
Chris Lively
+4  A: 

a GROUP BY clause will do the trick

SELECT staff.staff_id, COUNT(references_table.staff_id) 
FROM staff 
LEFT JOIN references_table USING (staff_id)
GROUP BY staff.staff_id
John Douthat
Yes it did! Thank you sir!
Matthew
Upvoting yours. It's a better answer.
Chris Lively