想要显示我每月和每年的所有支出,如下图 [][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 美元
二月 $200
7 月 90 美元
六月 $300
一月 100 美元
二月 $200
7 月 90 美元
六月 300 美元
最佳答案
似乎您需要按日期和月份订购
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
关于php - 按添加年份对所有每月收入组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52374077/