每月选择

时间:2010-10-06 11:12:09

标签: sql mysql

我有下表:

purchases(id, item, user, price, time);

time字段为timestamp

我需要一个每月返回一行的查询,该行将包含该月中每个项目的price总和。

2 个答案:

答案 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怎么办?