问题描述
用替换WHERE
子句HAVING
,如下所示:
SELECT o.ID , count(p.CAT)
FROM Orders o
INNER JOIN Products p ON o.P_ID = p.P_ID
GROUP BY o.ID
HAVING count(p.CAT) > 3;
HAVING
与相似WHERE
,两者均用于过滤结果记录,但HAVING
用于过滤汇总数据(GROUP BY
使用时)。
解决方法
此sql代码抛出一个
WHERE中不允许使用聚合函数
SELECT o.ID,count(p.CAT)
FROM Orders o
INNER JOIN Products p ON o.P_ID = p.P_ID
WHERE count(p.CAT) > 3
GROUP BY o.ID;
如何避免此错误?