。。按你问题中的那个语句来看,确实没必要。但你非得要用NOT EXISTS的话,,select * from tb1 where not exists (select 1 from tb1 where tb_id=1 and tb_id =2 and tb_id =5)。。没试啊,不知道能不能过。

解决方案 »

  1.   

    or吧,不是and吧
    另外,如果是or的话,好象一条都出不来另外,什么情况下最好用exists替换in,因为听说exists效率比in高,而且in是为了兼容低版本的,以后版本的未必用的上
      

  2.   

    楼上的,应该是
    select * from tb1 where not exists (select 1 from tb1 where tb_id=1 or tb_id =2 ortb_id =5)
      

  3.   

    扯淡的言论。exists 比in效率高? 语法根本不能决定效率的高低,查询是否快速要看你的表的配置。
      

  4.   

    tb_id 除了125,还有什么值?
      

  5.   


    select * from tb1 where not exists (select 1 from tb1 where tb_id=1 or tb_id =2 or tb_id =5)好像查不出结果啊!关注中。
      

  6.   

    8楼的,只是个列子,这只是个int自增型字段6楼的,这只是个列子,的确和表结构有关,但一般的简单表这两者差距也应该差不多吧,但如果复杂点怎么说,而且兼容性问题怎么说
      

  7.   

    何苦呢
    select * from tb1 where tb_id not in (1,2,5)
    这已经是最简单的写法了
      

  8.   

    select * 
    from tb1 a
    where not exists(select 1 from ( select 1 as id union select 2 union select 3) b where a.tb_id = b.id)
      

  9.   

    这是ANSI的标准语法,你指的兼容性问题是什么?NOT IN vs NOT EXISTS的区别,除了null问题之外,就是not in容易出现一些非逻辑性错误。USE Northwind;
    GO
    SELECT * FROM Shippers
    WHERE ShipperID NOT IN 
       (
       SELECT ShipperID FROM Orders
       )
    orders 中没有shipperid 字段,但语句不会报错,而是会转成NOT IN (Shippers.ShipperiD...)至於用哪一个效率高,要看执行计划是怎么生成的,单单说not in 比not exists差是毫无根据的。