列mysql的月份总和

时间:2014-01-24 11:58:23

标签: php mysql

您好我使用的是php和mysql。我有一个表,其中一列作为recdatetime作为datetime数据类型。

如果我在两个月之间搜索记录,我想要相同表格的数量列的总和。

如果我有以下记录。

quantity recdatetime
**44** `2014-01-01 16:53:06`
**14** ` 2014-01-21 16:53:06`
**10** `2013-12-21 16:53:06 `
**17** `2013-12-22 16:53:06 `
**29** `2013-11-20 16:53:06`

如果我在2013年11月到2014年1月期间进行搜索,我希望以下列方式输出。

November December January    
29        27       58

我想要Mysql查询以上输出。

3 个答案:

答案 0 :(得分:1)

从这开始:

SELECT SUM(quantity) AS s, DATE_FORMAT(recdatetime, '%M') AS m
FROM table_name
GROUP BY DATE_FORMAT(recdatetime, '%Y-%m')

答案 1 :(得分:0)

您可以使用月份(recdatetime),总和(数量)和逐月(recdatetime)。

Select year(recdatetime), month(recdatetime), sum(quantity) as total from x
group by year(recdatetime),month(recdatetime);

您将需要一年或者它也将总结不同年份的月份。你通常不希望这样。并添加where条件只能获得您想要的月份。希望你能靠自己做到。

可以在输出中完成切换行和列。

答案 2 :(得分:-1)

您可以尝试这样:

select sum(quantity) as sumQty,recdatetime FROM table_name group by Month(recdatetime
相关问题