gpt4 book ai didi

Mysql子查询group by错误

转载 作者:行者123 更新时间:2023-11-29 21:20:51 25 4
gpt4 key购买 nike

我想做一个子查询来找出两个信息。这是我得到的代码

SELECT b.boroughName, SUM(c.crimeCount), 
(SELECT SUM(c.crimeCount)
FROM FYP_Borough b JOIN FYP_Crime c on b.boroughID=c.boroughID
WHERE offenceID=8
GROUP BY boroughName) AS DrugUse
FROM FYP_Borough b JOIN FYP_Crime c ON b.boroughID=c.boroughID
GROUP BY b.boroughName

这是我得到的错误:子查询返回多于 1 行

我想按 boroughName 进行分组,因为否则它会为我的每个字段提供相同的值。我该怎么做?

最佳答案

SELECT b.boroughName, SUM(c.crimeCount), 
SUM(CASE WHEN offenceID=8 THEN c.crimeCount ELSE 0 END)
AS DrugUse
FROM FYP_Borough b JOIN FYP_Crime c ON b.boroughID=c.boroughID
GROUP BY b.boroughName

关于Mysql子查询group by错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734548/

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