MySQL include count 不返回计数为零的行

发布于 2024-12-19 13:40:27 字数 311 浏览 1 评论 0原文

我有以下 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 JOIN 和 RIGHT JOIN ,但都没有成功。有人还有其他建议吗?

I have the following MySQL code:

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

Unfortunately the count does not take into account rows which are 0, so for example c_id may have 10 rows but only 9 are displayed (because one of the e_id's count is zero).

I have tried both LEFT JOIN and RIGHT JOIN with no success. Does anyone have any other suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千里故人稀 2024-12-26 13:40:27

我怀疑你需要的是这样的:

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

I suspect that what you need is this:

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
东走西顾 2024-12-26 13:40:27
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
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
渡你暖光 2024-12-26 13:40:27

使用:

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

Use:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文