实施例:
假设我有表customerphonedetails
(customer_no
,phone_no
)。
data is like this :
customer_no =2 Phone_no=11
customer_no =2 Phone_no=12
customer_no =2 Phone_no=13
customer_no =1 Phone_no=11
customer_no =1 Phone_no=12
customer_no =1 Phone_no=13
customer_no =3 Phone_no=22
现在我想写一个查询,找到customer_no
全部 phone_no=(11,12,13)
。
预期结果是:Customer_no =(1,2)。
答案 0 :(得分:2)
您可以使用Phone_no
条款检查where
中为customer_no
提供的所有三个having
是否存在:
select customer_no
from customerphonedetails
where Phone_no in (11, 12, 13)
group by customer_no
having count(distinct Phone_no) = 3
答案 1 :(得分:0)
select DISTINCT customer_no
from table
where phone_no in(11,12,13)
应删除重复记录。
答案 2 :(得分:0)
这是执行此操作的另一种方法:
select distinct yes.customer_no
from
(select customer_no, count(*) over (partition by customer_no) as how_many_do_you_have
from marcin.marcin) as yes
where yes.how_many_do_you_have = (select count(distinct Phone_no) from marcin.marcin)