| iID | saleID | cusID | date | total | discount | advance | paid_status |
+-----+--------+-------+------------+-------+----------+---------+-------------+
| 16 | 1 | 1 | 2014-01-17 | 2400 | 100 | 1000 | 1 |
| 17 | 1 | 1 | 2014-02-17 | 240 | 100 | 1000 | 1 |
| 18 | 1 | 1 | 2013-03-17 | 280 | 100 | 1000 | 1 |
| 19 | 1 | 1 | 2014-04-18 | 120 | 100 | 1000 | 1 |
基本上我有上面显示的数据库。我想得到每个月的总和,但具体年份
当我使用此查询时,它可以正常运行,但由于我没有指定年份,因此它不会仅给出特定的年份
SELECT sum(total) FROM `invoice_reg` group by month(date) ;
但是当我使用where子句时,它没有工作给我一个错误
SELECT sum(total) FROM `invoice_reg` group by month(date) where YEAR(date) = 2014
我也尝试过这个。但是也是很好的
SELECT sum(total) FROM `invoice_reg` group by month(date) where DATE_FORMAT(date,'%Y')=2014
这是我得到的错误
check the manual that corresponds to your MySQL server version for the right syntax to use near 'where YEAR(date) = 2014
任何帮助将不胜感激
答案 0 :(得分:2)
尝试将group by
语句移动到命令的末尾
答案 1 :(得分:1)
答案 2 :(得分:0)
你可以这样试试
SELECT monthly_total FROM
(
SELECT
sum(total) as monthly_total,YEAR(date) as yr
FROM `invoice_reg`
GROUP BY month(date)
) t
WHERE yr = 2014
答案 3 :(得分:0)
SELECT sum(total) FROM `invoice_reg`
where YEAR(date) = year(now())
group by month(date)
答案 4 :(得分:0)
试试这个:
SELECT sum(total)
FROM invoice_reg
WHERE year(date) = 2014
GROUP BY month(date)