gpt4 book ai didi

mysql - 在联接中使用 Where 子句,使用 Group by 和 Order By

转载 作者:可可西里 更新时间:2023-11-01 07:47:45 25 4
gpt4 key购买 nike

我正在使用 MySQL 经典模型数据库。以下查询工作正常(注意 where 子句)

select 
customers.customerNumber as 'Customer ID',
customers.customerName as 'Customer Name',
count(orders.orderNumber) as 'Total Orders Placed'
from customers
left join orders on customers.customerNumber = orders.customerNumber
where customers.customerNumber > 200
group by
customers.customerNumber
order by
3 asc

但是下面会导致错误。目的是仅显示结果行集中那些下了 3 个以上订单的客户。我做错了什么?

select 
customers.customerNumber as 'Customer ID',
customers.customerName as 'Customer Name',
count(orders.orderNumber) as 'Total Orders Placed'
from customers
left join orders on customers.customerNumber = orders.customerNumber
where count(orders.orderNumber) > 3
group by
customers.customerNumber
order by
3 asc

MySQL 错误是:错误代码:1111。组函数的使用无效

最佳答案

聚合函数(COUNT()、AVG()、SUM()、 等)不能出现在 WHERE 子句中,因为它们是在计算时出现的。相反,它们属于 HAVING 子句:

select 
customers.customerNumber as 'Customer ID',
customers.customerName as 'Customer Name',
count(orders.orderNumber) as 'Total Orders Placed'
from customers
left join orders on customers.customerNumber = orders.customerNumber
group by
customers.customerNumber
HAVING count(orders.orderNumber) > 3
order by
3 asc

关于mysql - 在联接中使用 Where 子句,使用 Group by 和 Order By,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560510/

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