表 T1 
SID DD CC
012 89 50 
315 88 11 
014 99 44  
表 T2 
SID JJ YY
315 898 852 
014 78  221
求 結果 表T1.sid <> T2.SID 
SID DD CC
012 89 50

解决方案 »

  1.   

    select t1.* from t1 where sid not in (select sid from t2)
      

  2.   

    select t1.* from t1 where sid not in (select sid from t2)select t1.* from t1 where not exists (select 1 from t2 where t2.sid = t1.sid)
      

  3.   

    SELECT * FROM T1 WHERE SID NOT IN(SELECT SID FROM T2)
      

  4.   

    create table T1(SID varchar(10),DD int,CC int)
    insert into t1 values('012', 89 ,50)  
    insert into t1 values('315', 88 ,11)  
    insert into t1 values('014', 99 ,44 )  
    create table T2(SID varchar(10),jj int,yy int)
    insert into t2 values('315', 898, 852 ) 
    insert into t2 values('014', 78 ,221)goselect t1.* from t1 where sid not in (select sid from t2)
    /*
    SID        DD          CC          
    ---------- ----------- ----------- 
    012        89          50(所影响的行数为 1 行)
    */select t1.* from t1 where not exists (select 1 from t2 where t2.sid = t1.sid) 
     /*
    SID        DD          CC          
    ---------- ----------- ----------- 
    012        89          50(所影响的行数为 1 行)
    */drop table t1 , t2
      

  5.   

    --使用 left join
    select t1.* from t1 left join t2 on t1.sid=t2.sid where t2.sid is null
      

  6.   

    借用一下大乌龟的数据if object_id('T1') is not null
    drop table T1create table T1(SID varchar(10),DD int,CC int)
    insert into t1 values('012', 89 ,50)  
    insert into t1 values('315', 88 ,11)  
    insert into t1 values('014', 99 ,44 )  
    if object_id('T2') is not null 
    drop table T2
    create table T2(SID varchar(10),jj int,yy int)
    insert into t2 values('315', 898, 852 ) 
    insert into t2 values('014', 78 ,221)select * from T1 t where  t.SID in ((select sid from T1) except (select sid from T2))
      

  7.   

    select t1.* from t1 where sid not in (select sid from t2)
      

  8.   


    select SID,DD as DDorJJ,  CC as CCorYY from t1 where sid not in (select sid from t2)
    union 
    select SID,JJ as DDorJJ,  YY as CCorYY from t2 where sid not in (select sid from t1)
      

  9.   

    --create table T1(SID varchar(10),DD int,CC int)  
    --insert into t1 values('012', 89 ,50)    
    --insert into t1 values('315', 88 ,11)    
    --insert into t1 values('014', 99 ,44 )    
    --create table T2(SID varchar(10),jj int,yy int)  
    --insert into t2 values('315', 898, 852 )   
    --insert into t2 values('014', 78 ,221)
    --insert into t2 values('013', 78 ,221)select * from T1 where sid not in(
    select sid from T2
    )
    union 
    (
    select * from T2 where sid not in(
    select sid from T1)
    )