mysql - 使用 `count` 在 `group by` 上获取值为 0 的列
问题描述
我正在使用此查询来获取按cmp2
字段分组的行数,但我需要为每个获取一列,cmp2
即使它的总和结果是0
. 我不能这样理解:
SELECT CMP2, COALESCE(count(*), 0) as count
FROM datos_con851_0,
datos_con851_1
WHERE datos_con851_0.REGISTRO = datos_con851_1.REGISTRO
AND SPEEDER = 1
GROUP BY CMP2;
解决方案
你想要一个left join
,大概是这样的:
SELECT d0.CMP2, count(d1.REGISTRO) as count
FROM datos_con851_0 d0 left join
datos_con851_1 d1
on d0.REGISTRO = d1.REGISTRO AND
d1.SPEEDER = 1 -- just guess this comes from `d1`
GROUP BY d0.CMP2;
推荐阅读
- c - $HOME 时出现 getcwd 错误
- kotlin - 3次后如何设置Kotlin while循环停止?
- c++ - Void 值没有被忽略(试图将 void 分配给非 void 变量?)
- javascript - Winston Logger 可以用在前端进行日志记录吗?
- docker - 使用 Docker 运行 Jupyterhub 时连接重置
- javascript - 语法:针对不同状态属性的 React 中的 handleChange() 函数
- python - 将 tf.py_func 添加到梯度不工作的 Tensorflow 图
- swift3 - 如何使用 Alamofire 从 JSON 获取数据?
- jenkins - 抑制 Jenkins 中的脚本化管道输出
- swift - 模拟器黑屏