我有下表:
purchases(id, item, user, price, time);
time
字段为timestamp
。
我需要一个每月返回一行的查询,该行将包含该月中每个项目的price
总和。
答案 0 :(得分:10)
试试这个:
SELECT MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY MONTH(`time`)
如果您有一年以上的数据,您可能还希望将以下年份包含在您的论坛中:
SELECT YEAR(`time`) AS year, MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY YEAR(`time`), MONTH(`time`)
答案 1 :(得分:2)
GROUP BY YEAR(DATE(time)) ASC, MONTH(DATE(time)) ASC
怎么办?