gpt4 book ai didi

mysql - 如何避免错误 "aggregate functions are not allowed in WHERE"

转载 作者:IT老高 更新时间:2023-10-28 12:58:08 25 4
gpt4 key购买 nike

这个sql代码抛出一个

aggregate functions are not allowed in 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;

我怎样才能避免这个错误?

最佳答案

HAVING替换WHERE子句,像这样:

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;

HAVINGWHERE 类似,都用于过滤结果记录,但 HAVING 用于过滤聚合数据(当使用 GROUP BY 时)。

关于mysql - 如何避免错误 "aggregate functions are not allowed in WHERE",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20991729/

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