2009-04-30 135 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