每月

2010-10-06 68 views
8

選擇我有如下表:每月

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

time字段是timestamp

我需要一個每月返回一行的查詢,該行將包含該月中每個項目的price的總和。

回答

9

試試這個:

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`) 
2

什麼GROUP BY YEAR(DATE(time)) ASC, MONTH(DATE(time)) ASC