MySQL包括count不返回计数为零的行

时间:2011-12-06 13:28:09

标签: mysql join count row

我有以下MySQL代码:

SELECT COUNT(e.eid) AS cnt, e.c_id, c.cdesc 
FROM e 
JOIN c on c.c_id = e.c_id 
GROUP by c.c_id

不幸的是,count没有考虑0的行,因此例如c_id可能有10行但只显示9行(因为其中一个e_id计数是零)。

我已经尝试了LEFT JOINRIGHT JOIN但没有成功。有没有人有任何其他建议?

3 个答案:

答案 0 :(得分:2)

我怀疑你需要的是:

SELECT COUNT(*) AS cnt, c.c_id, c.cdesc 
FROM c 
LEFT JOIN e on c.c_id = e.c_id 
GROUP by c.c_id

答案 1 :(得分:1)

SELECT COUNT(e.eid) AS cnt, e.c_id, c.cdesc 
FROM c
left JOIN e on c.c_id = e.c_id 
GROUP by c.c_id

答案 2 :(得分:0)

使用:

SELECT COUNT(*) AS cnt, e.c_id, c.cdesc 
FROM e 
JOIN c on c.c_id = e.c_id 
GROUP by c.c_id