表如下,想验证一下,同一个col1下,不允许出现同样的col2,例如第4,5行的数据就是非法的,,怎样给查出来呢?
id  col1 col2
-----------
1  001  a
2  001  b
3  001  c
4  002  a
5  002  a
6  002  b

解决方案 »

  1.   

    select * from tb a where exists(select 1 from tb where col1=a.col1 and col2=a.col2
      

  2.   

    select * from tb a where exists(select 1 from tb where col1=a.col1 and col2=a.col2)
      

  3.   

     把col1、col2同时作为主键使用
      

  4.   


    这个不对哦这样的话所有的数据都会出来了 因为select 1 from tb where col1=a.col1 and col2=a.col2这个条件可以自己等于自己i的select * from tb a where exists(select 1 from tb where id<>a.id and col1=a.col1 and col2=a.col2)
    再加一个条件嘿嘿
      

  5.   

    select * from tb a where exists(select 1 from tb where id<>a.id and col1=a.col1 and col2=a.col2)This is right
      

  6.   

    select * from aa a where  exists(select 1 from aa where a.id<> aa.id and a.col1=aa.col1
    and a.col2=aa.col2)
    这段代码是正确的
      

  7.   


    select * from tb
    where  exists
    (
    select 1 from
    (
    select   col1,col2 from tb group by col1,col2
    having  count(id)>1
    ) as Temp where tb.col1=temp.col1 and tb.col2=col2
    )
      

  8.   


             select col1,col2,count(*) from 表 
              group by col1,col2 having count(*) > 1