gpt4 book ai didi

mysql - 如何为语句 SELECT COUNT(*) as num... 编写子句 : WHERE num =. ..?

转载 作者:行者123 更新时间:2023-12-01 00:26:36 24 4
gpt4 key购买 nike

我有声明

SELECT COUNT(*) as num_requests, ip_address FROM requests
GROUP BY ip_address
ORDER BY num_requests DESC;

当我使用以下子句 WHERE num_requests = 3 时出现语法错误

SELECT COUNT(*) as num_requests, ip_address 
FROM requests
WHERE num_requests = 3
GROUP BY ip_address
ORDER BY num_requests DESC;

“where 子句”中的未知列“num_requests”

有什么方法可以使用此 WHERE 子句而不用对上面返回的整个结果集编写嵌套查询或连接语句?

最佳答案

你需要having :)

SELECT COUNT(*) as num_requests, ip_address FROM requests
GROUP BY ip_address
HAVING COUNT(*) = 3
ORDER BY num_requests DESC;

添加关于原因的注释 WHERE无法使用但HAVING : 聚合如 SUM, MIN,MAX,AVG,COUNTWHERE不工作。所以你必须使用后者。

关于mysql - 如何为语句 SELECT COUNT(*) as num... 编写子句 : WHERE num =. ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582320/

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