好的,我有 2 张 table
第一个名为 msg
,另一个名为 msg_t
msg
(id、send_type、..)
msg_t
(id、msg_id、send_time)
我想做的是获取 send_type = 1 的所有 msg
行
并计算每个 msg
的 msg_t
条目并按月分组
我该怎么做?
最佳答案
SELECT a.ID, MONTHNAME(b.send_time), COUNT(b.msg_id) totalCount
FROM msg a
LEFT JOIN msg_t b
ON a.ID = b.msg_id
WHERE a.send_type = 1
GROUP BY a.ID, MONTH(b.send_time)
通过使用LEFT JOIN
,表msg_t
中没有记录的msg.ID
将显示零值
关于mysql - SQL 从 2 个表中选择并按月分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14438921/