想像下面的图[] [2]一样显示每月和每年的所有支出,并且代码如下所示
SELECT
monthname(date_added) as monthname,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes GROUP BY year ORDER BY monthname
2017年
一月$ 100
2月$ 200
7月$ 90
6月$ 300
答案 0 :(得分:0)
似乎您需要按日期和月份订购
select t.year, t.monthname, t.amounts
from(
SELECT monthname(date_added) as monthname,
month(date_added) month,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes
GROUP BY year, month, monthname
)
ORDER BY t.year, t.month
或者如果您需要今年和前一年的两个分开的结果
对于实际年份,您可以
select t.year, t.monthname, t.amounts
from(
SELECT monthname(date_added) as monthname,
month(date_added) month,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes
where year(date_added) = year(curdate())
GROUP BY year, month, monthname
)
ORDER BY t.year, t.month
上一年您可以
select t.year, t.monthname, t.amounts
from(
SELECT monthname(date_added) as monthname,
month(date_added) month,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes
where year(date_added) = year(curdate()) -1
GROUP BY year, month, monthname
)
ORDER BY t.year, t.month