gpt4 book ai didi

mysql - 如果某个月份没有记录,如何获取 0 条记录?

转载 作者:行者123 更新时间:2023-11-29 16:38:08 25 4
gpt4 key购买 nike

我有查询,显示最近 12 个月的记录,它对我来说工作正常,但我想显示那个没有记录的月份,如果特定月份没有记录,那么我想显示 0 条记录对于那个月,我在这里添加了我的查询

SELECT `amount`, CONVERT_TZ(created, "+00:00", "-05:00") as created, 
MONTHNAME(CONVERT_TZ(created, "+00:00", "-05:00")) as month,
`sponsorion_fees`, `processor_fees`, `amount_after_fees`,
SUM(amount_after_fees) as total FROM `transaction`
WHERE `record_owner_user_id` = '50' AND `is_one_time_purchase` = 'Y'
AND CONVERT_TZ(created,"+00:00","-05:00") <= "2018-11-26 07:08:24"
and CONVERT_TZ(created,"+00:00","-05:00") >=
Date_add("2018-11-26 07:08:24",interval - 12 month)
GROUP BY `month` ORDER BY `id` ASC

谁能帮我解决这个问题吗?

最佳答案

尝试IFNULL :

SELECT `amount`, CONVERT_TZ(created, "+00:00", "-05:00") as created,
MONTHNAME(CONVERT_TZ(created, "+00:00", "-05:00")) as month,
`sponsorion_fees`, `processor_fees`, `amount_after_fees`,
SUM(IFNULL(amount_after_fees, 0)) as total FROM `transaction`
WHERE `record_owner_user_id` = '50'
AND `is_one_time_purchase` = 'Y'
AND CONVERT_TZ(created,"+00:00","-05:00") <= "2018-11-26 07:08:24"
and CONVERT_TZ(created,"+00:00","-05:00") >= Date_add("2018-11-26 07:08:24",interval - 12 month)
GROUP BY `month`
ORDER BY `id` ASC

也可以尝试COALESCE :

SELECT `amount`, CONVERT_TZ(created, "+00:00", "-05:00") as created,
MONTHNAME(CONVERT_TZ(created, "+00:00", "-05:00")) as month,
`sponsorion_fees`, `processor_fees`, `amount_after_fees`,
COALESCE(SUM(amount_after_fees),0) as total FROM `transaction`
WHERE `record_owner_user_id` = '50'
AND `is_one_time_purchase` = 'Y'
AND CONVERT_TZ(created,"+00:00","-05:00") <= "2018-11-26 07:08:24"
and CONVERT_TZ(created,"+00:00","-05:00") >= Date_add("2018-11-26 07:08:24",interval - 12 month)
GROUP BY `month`
ORDER BY `id` ASC

关于mysql - 如果某个月份没有记录,如何获取 0 条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53480881/

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