gpt4 book ai didi

mysql - 如何对两个 SUM 的 SQL 求和结果?

转载 作者:行者123 更新时间:2023-12-01 00:18:03 25 4
gpt4 key购买 nike

使用此查询,我尝试对两个 SUM 函数的结果求和:

select 
DAY(created_at) AS day,
SUM(if(status = '1', 1, 0)) AS result,
SUM(if(status = '2', 1, 0)) AS noresult,
SUM(result + noresult)
from `clients` where `doctor_id` = 2 and MONTH(created_at) = MONTH(CURRENT_TIMESTAMP) group by `day`

我尝试在这一行中这样做:

SUM(result + noresult)

最佳答案

试试这个:

select 
DAY(created_at) AS day,
SUM(if(status = '1', 1, 0)) AS result,
SUM(if(status = '2', 1, 0)) AS noresult,
SUM(if(status in ('1', '2'), 1, 0))
from `clients`
where `doctor_id` = 2 and MONTH(created_at) = MONTH(CURRENT_TIMESTAMP)
group by `day`

关于mysql - 如何对两个 SUM 的 SQL 求和结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46456128/

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