gpt4 book ai didi

mysql - 来自同一表的多个求和查询和多个 where 条件

转载 作者:行者123 更新时间:2023-11-29 05:47:39 24 4
gpt4 key购买 nike

我有 2 个查询要放入其中一个,然后按 fName 分组。我尝试了数百次,但无法正常工作。

 SELECT fName, code, 
SUM(totalOr) AS totOr
FROM person
WHERE code = '4'
GROUP BY fName

SELECT fName, code,
SUM(totalPl) AS totPl
FROM person
WHERE code = '1'
GROUP BY fName

最佳答案

你可以使用CASE语句

SELECT 
fName,
code,
SUM(CASE WHEN code = 1 THEN totalOr ELSE 0 END) AS totOr,
SUM(CASE WHEN code = 4 THEN totalPl ELSE 0 END) AS totP1
FROM
person
WHERE
code IN ('1' , '4')
GROUP BY fName, code;

关于mysql - 来自同一表的多个求和查询和多个 where 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58285196/

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