简体   繁体   English

sql查询中的having子句返回无效

[英]having clause in sql query returns invalid

I'm trying to retrieve all the IDs that fit the following criteria: 我正在尝试检索符合以下条件的所有ID:

  1. lives on specific street 住在特定的街道上
  2. has two telephone numbers 有两个电话号码
  3. has made an order 已下订单
  4. is female 是女性

select id from Customer 
join Address on Address.customer_id = Customer.id
join Orders on Orders.customer_id = Customer.id
join Gender on Gender.customer_id = Customer.id
join Telephone on Telephone.customer_id = Customer.id
where Address.street_name ='MainStreet' 
and Orders.customer_id is not null
and Gender.gender ='Female'
group by Telephone.customer_id
having count(Telephone.customer_id) = 2

When I run this, I receive "Column or expression is not valid" When I remove the group by and having, it works fine. 运行此命令时,我收到“列或表达式无效”的提示当我删除并删除该组时,它可以正常工作。

Can someone help me with this please? 有人可以帮我吗?

Try this one 试试这个

select id from Customer 
join Address on Address.customer_id = Customer.id
join Orders on Orders.customer_id = Customer.id
join Gender on Gender.customer_id = Customer.id
join (
select customer_id from Telephone
group by customer_id
having count(customer_id) = 2
) AS Telephone on Telephone.customer_id = Customer.id
where Address.street_name ='MainStreet' 
and Orders.customer_id is not null
and Gender.gender ='Female'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM