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

时间:2014-01-15 11:06:57

标签: mysql sql

我正在尝试检索符合以下条件的所有ID:

  1. 生活在特定的街道上
  2. 有两个电话号码
  3. 已下订单
  4. 是女性
  5. 
    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
    

    当我运行它时,我收到“列或表达式无效” 当我删除该组时,它可以正常工作。

    有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

试试这个

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'