我的查询是
SELECT MONTH(now()) as m, SUM(total) as grandtotal
FROM tb
group BY YEAR(now()), MONTH(now())
我正在搜索如何获得今年和本月的总数,但我的总计也是去年的总数。例如,我有2018年第8个月= 20和2017年第8个月= 40的数据。 它将总结所有。所以我的总数将是60而不是20。
答案 0 :(得分:0)
仅按分组使用月-将按月级别给您总和,但是如果按分组也按年将显示逐年总计和
SELECT MONTH(now()) as m, SUM(total) as grandtotal
FROM tb
group BY MONTH(now())
答案 1 :(得分:0)
Based on answer from last time
select year(date) as y, month(date) as m, sum(paid) as p
from table
where year(now()) = year(date) and month(now()) = month(date)
group by year(date), month(date)
请注意,如果表定义不同,则列名也可能会不同。
答案 2 :(得分:0)
您应该按字段的值而不是常量进行分组。
SELECT MONTH(YourDateField) as m, SUM(total) as grandtotal
FROM tb
WHERE YEAR(YourDateField) = YEAR(now())
GROUP BY YEAR(YourDateField), MONTH(YourDateField)