gpt4 book ai didi

php - 按添加年份对所有每月收入组进行排序

转载 作者:行者123 更新时间:2023-11-29 16:59:26 24 4
gpt4 key购买 nike

想要显示我每月和每年的所有支出,如下图 [][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

<小时/>2018年
一月 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com