【发布时间】:2017-12-24 09:43:34
【问题描述】:
请问我如何对我的 MySQL 表中过去 3 个月的记录求和,但按月分组。
我想要这样的东西:
select SUM(amount) from table where.....
group by month
我正在执行以下操作,但没有返回任何结果
SELECT MONTHNAME(a.created_at) MONTH, YEAR(a.created_at) YEAR, SUM(a.credit) as credit, SUM(a.debit)
FROM telco_transactions AS a
WHERE a.telco_id = '1' and DATE(a.created_at) = DATE_ADD(NOW(), INTERVAL -3 MONTH)
GROUP BY MONTHNAME(a.created_at), YEAR(a.created_at);
【问题讨论】:
-
看起来很好,除了 = 可能应该是 >=
-
当我设为 '>=' 时,它会返回 11 月、12 月、1 月的记录,而不是 10 月、11 月和 12 月的记录
-
不,不是,当您选择 now() 时会得到什么?