我想计算响应表中未读消息的数量。问题是,当根据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
发布于 2019-05-07 08:19:50
将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
https://stackoverflow.com/questions/56018315
复制