MySQL通过查询包括组中的0个计数项

时间:2017-06-30 00:08:57

标签: mysql sql

我有类似的查询:

SELECT Zip, COUNT(*) as count 
FROM people 
WHERE specialty='Driver' 
GROUP BY Zip 
ORDER BY Zip

它使用0计数过滤掉结果,但即使计数为0,我仍然需要所有分组的拉链。

1 个答案:

答案 0 :(得分:4)

您可以使用条件聚合

SELECT Zip, SUM(specialty = 'Driver') as count 
FROM people 
GROUP BY Zip 
ORDER BY Zip