gpt4 book ai didi

mysql - 有没有办法包括所有案例的总和?

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:18 25 4
gpt4 key购买 nike

给定的SQL:有没有办法把结果集的总数带进来?

SELECT 
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
FROM
Table1
WHERE
id = 2;

结果:

Open,Close
5,5

期望的结果:

Open,Close,Total
5,5,10

最佳答案

只需添加另一个 case 语句

SELECT 
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
SUM(CASE
WHEN status IN (3, 4) THEN 1
ELSE 0
END) AS Total
FROM
Table1
WHERE
id = 2;

关于mysql - 有没有办法包括所有案例的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51567864/

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