mysql 按日期选择总和组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/937652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:23:47  来源:igfitidea点击:

mysql select sum group by date

mysqlselectgroup-by

提问by Simon Fox

Quick question, I have the following table

快速提问,我有下表

+-------------+---------------------+
| 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 | 
+-------------+---------------------+

I would like to add up the total for each month and group the total by month. So for instance Jan-> 138 Feb-> 88.2 Apr-> 29.84

我想把每个月的总数加起来,然后按月对总数进行分组。所以例如Jan-> 138 Feb-> 88.2 Apr-> 29.84

Any clues about it. Thanks

任何有关它的线索。谢谢

回答by Simon Fox

This solution will give you the month name as a column of your resultset, followed by the total as required.

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

SELECT MONTHNAME(o_date), SUM(total) 
FROM theTable
GROUP BY YEAR(o_date), MONTH(o_date)

回答by Todd Gardner

select year(o_date), month(o_date), sum(total)
from table
group by year(o_date), month(o_date);

回答by Fazlul Haq

Get Month And Year wise data from MySQL database:

从 MySQL 数据库中获取月和年明智的数据:

SELECT MONTHNAME(o_date), YEAR(o_date), SUM(total) 
FROM the_table 
GROUP BY YEAR(date), MONTH(date)

回答by James Skidmore

SELECT SUM(total)
FROM table
GROUP BY MONTH(o_date)

回答by umang

Try Group BY, like this:

试试Group BY,像这样:

select count(A_id) As Tid,Day(CrDate) from Ap Group By Day(CrDate)

回答by hugronaphor

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;