一个表,字段如下A01,A02
A01      A02
20090001   Y
20090002   Y
20090002   N
20090002   V
20090003   R
20090004   U
20090005   V
20090005   Y
……
要返回结果如下:
A01      A02
20090001   Y
20090003   R
20090004   U
20090005   V
20090005   Y
……当A02中有特定值如“N”时,则排除掉A01中与之相同的所有行。

解决方案 »

  1.   

    select  *  from 表 where A01  not in  (select  A01  from 表  where  A02 ='N')
      

  2.   

    SELECT * FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE A02='N')
      

  3.   

    select * from 表 a where not exists(select * from 表 where a01=a.a01 and a02='N')
      

  4.   

    not exists 和not in 哪个更有效率呢?
      

  5.   

    SELECT * FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE A02='N')
      

  6.   

    select *
    from table
    where table.A01 !=
    (
    select table.A01
    from table
    where table.A02 = 'N'
    )