此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;
如何避免此错误?
用替换WHERE子句HAVING,如下所示:
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使用时)。
GROUP BY