PHP如何从mysql获得月份记录

时间:2017-05-17 11:30:53

标签: php mysql sum

我需要你的帮助。我在mysql中有以下数据,我的要求是按月计算金额。

Amount == Date
5000   || 01-05-2017 //Month May
6000   || 05-05-2017  //Month May
7000   || 20-05-2017  //Month May
10000  || 01-06-2017 //Month June
12000  || 05-06-2017  //Month June
13000  || 20-06-2017  //Month June

预期结果:

Sum = May 18000
Sum = June 35000
Sum = July 111111

2 个答案:

答案 0 :(得分:0)

SELECT 
  SUM(AMOUNT),
  DATE_FORMAT(DATE_COLUMN,'%M') 
FROM TABLENAME 
GROUP BY DATE_FORMAT(DATE_COLUMN,'%m')

尝试以上代码。

希望这会有所帮助。

答案 1 :(得分:-1)

SUM汇总功能与GROUP BY& dateTime functions

SELECT
    SUM(AmountColumn),
    DATE_FORMAT(record_date, '%M %Y') AS MONTH
FROM
    table_name
GROUP BY
    DATE_FORMAT(record_date, '%M %Y')