mysql:本月,上个月和每个月的时间戳

时间:2013-05-04 08:22:39

标签: mysql

我有以下mysql表

amount  timestamp
3       1367638029
4       1367638029
5       1366445119
8       1366445200

如何根据本月(不是过去30天)以及上个月(如下所示)返回金额(金额)

            total amount
This month  7
Last month  13

1 个答案:

答案 0 :(得分:1)

这可以这样做:

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