Mysql子查询组出错

时间:2016-03-01 22:06:39

标签: mysql subquery

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

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进行分组,否则它会为每个字段提供相同的值。我该怎么做?

1 个答案:

答案 0 :(得分:0)

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