在表格中查找所有具有电话号码的客户

时间:2015-07-22 07:39:59

标签: sql oracle

实施例: 假设我有表customerphonedetailscustomer_nophone_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)。

3 个答案:

答案 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)