表结构如下:
name    col
a       1
a       2
a       2
b       3
b       4
c       1
d       2如果传入的值为:1,2
那么分组查询满足1,2在col的数据
只有a满足
怎么查询?

解决方案 »

  1.   

    SELECT DISTINCT name from tb where col in(1,2)
      

  2.   

    SELECT name 
    from tb where col in(1,2)
    group by name
    having count(distinct col) = 2
      

  3.   

    or:SELECT name  
    from tb a
    where col =1
    and exists (
    select 1 from tb
    where name = a.name
    and col = 2
    )or:SELECT distinct a.name  
    from tb a,tb b
    where a.col =1
    and b.name = a.name
    and b.col = 2
      

  4.   

    select name from tb where col betwen 1 and 2; --between包括临界么?
    SELECT name from tb where col in(1,2);
      

  5.   

    declare @str varchar(30)='1,2'
    select name from tb a
    where (select rtrim(col)+',' from tb where name=a.name for xml path(''))=@str+','