2009-04-30 136 views
1

這個查詢:COUNT(*)和LEFT JOIN

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

返回此:

staff_id COUNT(references_table.staff_id) 
1   2 

我怎樣才能得到它作爲計數返回0 staff_ids沒有任何引用?

回答

1

嘗試左外連接。

+0

'外部' 的 'LEFT OUTER JOIN' 是在MySQL語法 – 2009-04-30 21:24:08

+0

我是LEFT JOIN和LEFT OUTER JOIN是同義詞的印象是多餘的。 – Matthew 2009-04-30 21:24:54

5

一個GROUP BY條款會做的伎倆

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