id       name      type      phone
1         2         1         2
2         2         2         3
3         2         3         4
4         3         3         3
5         2         6         3
6         3         5         4
7         3         7         4
8         2         7         5
9         4         9         7
比如这个的数据类型
怎么查找同时拥有三个phone(2,3,4)的name
怎么查找同时拥有四个phone(2,3,4,5)的name要是这样的话
select name from table
where phone in (2,3,5) 
group by name 
having count(*)=3
也不对,记录可以能有重复!!请问sql怎么写!!
谢谢

解决方案 »

  1.   

    select name
    from table
    where phone in (2,3,5) 
    group by name 
    having count(distinct phone )=3
      

  2.   

    select name from table 
    where phone in (2,3,4,5) 
    group by name 
    having count(distinct phone)=4
      

  3.   

    去掉group by name不就允许重复了....
      

  4.   

    or
    select name from (
    select name,phone from tt 
    where phone in (2,3,4,5) 
    group by name ,phone)
    group by name having count(*)=4