47

快速提问,我有下表

+-------------+---------------------+
| total       | o_date              |
+-------------+---------------------+
|          35 | 01-11-2009 19:32:44 | 
|        41.5 | 01-12-2009 22:33:49 | 
|        61.5 | 01-23-2009 22:08:24 | 
|          66 | 02-01-2009 22:33:57 | 
|       22.22 | 02-01-2009 22:37:34 | 
|       29.84 | 04-20-2009 15:23:49 | 
+-------------+---------------------+

我想把每个月的总数加起来,然后按月分组。因此,例如1 月- > 2 月 138 日-> 4 月 88.2 日-> 29.84

关于它的任何线索。谢谢

4

6 回答 6

91

此解决方案将为您提供月份名称作为结果集的列,然后根据需要提供总计。

SELECT MONTHNAME(o_date), SUM(total) 
FROM theTable
GROUP BY YEAR(o_date), MONTH(o_date)
于 2009-06-02T02:17:21.490 回答
12
select year(o_date), month(o_date), sum(total)
from table
group by year(o_date), month(o_date);
于 2009-06-02T02:15:25.660 回答
6

从 MySQL 数据库中获取月份和年份数据:

SELECT MONTHNAME(o_date), YEAR(o_date), SUM(total) 
FROM the_table 
GROUP BY YEAR(date), MONTH(date)
于 2015-03-30T10:20:32.817 回答
2
SELECT SUM(total)
FROM table
GROUP BY MONTH(o_date)
于 2009-06-02T02:15:36.953 回答
0

试试Group BY,像这样:

select count(A_id) As Tid,Day(CrDate) from Ap Group By Day(CrDate)
于 2014-03-20T09:20:08.333 回答
0
SELECT year(FROM_UNIXTIME(created)) year, month(FROM_UNIXTIME(created)) month, sum(amount) total
FROM {my_table}
GROUP BY year, month
ORDER BY year, month;
于 2018-06-19T20:04:56.513 回答