2013-05-04 92 views
-1

我有以下MySQL表MySQL的:每月時間戳本月,上月和

amount timestamp 
3  1367638029 
4  1367638029 
5  1366445119 
8  1366445200 

我如何返回基於這個月的總和(金額)(不過去30天),上月類似下面

  total amount 
This month 7 
Last month 13 

回答

1

這canbe做這樣:

SELECT SUM(AMOUNT) 
FROM yourtable 
WHERE MONTH(FROM_UNIXTIME(timestamp)) IN/BETWEEN ... -- consider YEAR(...)as well 
GROUP BY MONTH(FROM_UNIXTIME(timestamp))