我有这张表:
+----+---------------------+---------+---------+ | id | date | amounta | amountb | +----+---------------------+---------+---------+ | 1 | 2014-02-28 05:58:41 | 148 | 220 | +----+---------------------+---------+---------+ | 2 | 2014-01-20 05:58:41 | 50 | 285 | +----+---------------------+---------+---------+ | 3 | 2014-03-30 05:58:41 | 501 | 582 | +----+---------------------+---------+---------+ | 4 | 2014-04-30 05:58:41 | 591 | 53 | +----+---------------------+---------+---------+ | 5 | 2013-05-30 05:58:41 | 29 | 59 | +----+---------------------+---------+---------+ | 6 | 2014-05-30 05:58:41 | 203 | 502 | +----+---------------------+---------+---------+
我想对每一行的所有列求和,其中年 = 2014 年和月= 3(一月、二月、三月)。
编辑: 我不想选择季度。我希望能够在几个月之间进行选择。例如从一月到四月,或从三月到九月。
我该如何执行此操作?
您可以使用日期函数,QUARTER()并且YEAR()
SELECT SUM(amounta), SUM(amountb) FROM t WHERE YEAR(`date`)=2014 AND QUARTER(`date`)=1
这将适合季度查询。但是对于一个月的间隔,当然,使用MONTH():
SELECT SUM(amounta), SUM(amountb) FROM t WHERE YEAR(`date`)=2014 AND MONTH(`date`)>=1 AND MONTH(`date`)<=8