mysql - 如果 id 不为空,Mysql 计数
问题描述
我想从响应表中计算未读消息。问题是,当根据where语句中的id不存在消息时,它会计数为0并返回一行。该行不得返回。任何建议如何解决这个问题?
SELECT m.*, COUNT(mr.id) as total_unread
FROM `message` m
LEFT JOIN message_response mr ON (mr.message_id = m.id) AND mr.read = 0
WHERE m.performance_report_id = :id
如果没有找到消息,上述语句将返回。
NULL NULL NULL NULL NULL NULL NULL 0
解决方案
把你m.performance_report_id = :id
的ON Clause
而不是where
SELECT m.*, COUNT(mr.id) as total_unread
FROM `message` m
LEFT JOIN message_response mr ON (mr.message_id = m.id) AND mr.read = 0
and m.performance_report_id = :id
推荐阅读
- php - 在下拉列表中显示数据并选择编辑选项
- mysql - 如何使用外键 Spring Boot Java MySQL 将数据插入多个表
- sql - 如何在 h2 db 中修复“不明确的列名”
- c - 我的“猜密码”不起作用,如何解决?
- python - 如何使用 gzip 有效地将许多小文件压缩成许多小的 .tar.gz 文件?
- java - 如何将字符串转换为浮点数组?
- divio - Divio 应用程序冻结并停止工作
- ios - 无法将我的 Codename One iOS 应用程序连接到我的服务器
- c# - 做一个课堂项目,设法得到一些稳定的代码。但是它没有按预期计算。为什么以及什么会解决它?
- numpy - 如何在 np.reshape 期间指定顺序