在子查询的时候 想 not  in  (字段1,字段2) 可以么?
或者怎么写呢?

解决方案 »

  1.   

    not in 字段1 or not in 字段2
      

  2.   

    不能。
    如果楼主想要查询不在字段1或者不在字段2中的记录,可以采取下面的方式
    SELECT .....
    FROM LI 
    WHERE ID NOT IN 
    (SELECT ID1 AS ID FROM LI1 UNION
     SELECT ID2 FROM LI1)
      

  3.   

    用not exists(select 字段1,字段2 from tb)
      

  4.   

    可以啊 
    select * from tb where id not in(select 字段1 from .....)  and id not in(select 字段2 from .....) 
      

  5.   


    if object_id('[tab]') is not null drop table [tab]
    create table [tab]([no] int,[partno] int,[other] varchar(5),[num1] int)
    insert [tab]
    select 1,2,'abcde',2 union all
    select 1,2,'mmm',0 union all
    select 1,3,'kkk',1 union all
    select 1,4,'mkl',5select * from tab where partno not in(2,4)
    /*
    no          partno      other num1        
    ----------- ----------- ----- ----------- 
    1           3           kkk   1(所影响的行数为 1 行)*/select * from tab t where partno in (select partno from tab where partno not in(2,4))
    /*
    no          partno      other num1        
    ----------- ----------- ----- ----------- 
    1           3           kkk   1(所影响的行数为 1 行)*/
      

  6.   

    not in 字段肯定不行。
    not in()这里应该是一个集合。
    你要么把not in 的那个集合表示出来
    select a from table where a not in
    (select 字段1 from table union all
    select 字段2 from table
    )s
      

  7.   

    我知道 not in  2个字段 肯定是不行 ,我想知道怎么写比较好,
    我现在是 select × from t1 where 字段1 not in (select temp1 from t2) and 字段1 not in (select temp2 from t2)
    但是我感觉不效率 因为后面的子查询 条件很多, 语句很长。 想知道怎么写比较好,
      

  8.   

    select * from t1 where not exists(select temp1,temp2 from t2) 
      

  9.   

    TRY
    select × 
    from t1
    where not exists(
      select 1
      from t2
      where temp1=t1.字段1 or temp2=t1.字段1)
      

  10.   

    1.NOT EXISTS
    SELECT * FROM TB t WHERE NOT EXISTS(SELECT * FROM TB WHERE [字段1]=t.[字段1] AND [字段2]=t.[字段2])
    2.如果不需要太精确地情况下可以使用CHECKSUM。
    SELECT * FROM TB WHERE CHECKSUM([字段1],[字段2]) NOT IN (SELECT CHECKSUM([字段1],[字段2] FROM TB)
      

  11.   

    MODIFY:
    select × 
    from t1
    where not exists(
      select 1
      from t2
      where temp1=t1.字段1 
        and temp2=t1.字段1)or应该用and
      

  12.   

    not exists  这个我在试 , 不过好像不是用or 吧 。not exists 放在hibernate  里面好像报错 
      

  13.   

    select × from t1 where 字段1 not EXISTS (select temp1, temp2 from t2) 
    这样写可以么?
      

  14.   

    补充:
    EXISTS                Returns TRUE if a subquery returns at least one row. 
      

  15.   


    SORRY 我误导了你
    应该用
    SELECT * FROM TB t WHERE NOT EXISTS(SELECT * FROM TB WHERE [字段1]=t.[字段1] AND [字段2]=t.[字段2])
      

  16.   

    用这个
    select * from t1 where Not exists (select * from t2 where t1.字段1=temp1 or t1.字段1=temp2  )
      

  17.   

    [Quote=引用 13 楼 hery2002 的回复:]
    1.NOT EXISTSSQL codeSELECT * FROM TB t WHERE NOT EXISTS(SELECT * FROM TB WHERE [字段1]=t.[字段1] AND [字段2]=t.[字段2])这个我试过了,顶