有如下两个表
tb1
col   col1 
abc   111   
cde   111   
qaz   222    
tgb   222 tb2
col2   col3 
abc    111  
qaz    222  我怎么查询TB1表哪些数据在TB2表是没有的

解决方案 »

  1.   

    select  *  from TB1  where not  exsits  (select  *  from  TB2)
      

  2.   

    select * from tb1  where not  exsits  (select  *  from  tb2)
      

  3.   

    select * from tb1  where not  exists  (select  *  from  tb2)
      

  4.   

    可能描述错了,这两个表应该是这样的
    如下:
    tb1
    guid   col  col1
    dadf   abc  111 
    1erw   cde  111 
    21qe   qaz  222   
    qweq   tgb  222tb2
    guid   col2  col3
    w5wr   abc    111 
    w34f   qaz    222  
      

  5.   


    declare @t1 table (col varchar(5),  col1 int)
    insert into @t1 select 
    'abc',  111  union all select
    'cde',  111  union all select
    'qaz',  222   union all select 
    'tgb',  222 declare @t2 table (col2 varchar(5),  col3 int)
    insert into @t2 select 
    'cde',  111  union all select
    'qaz',  222  
    select * from @t1 
    where  not exists(select * from @t2  where col=col2 and col1=col3) 
    col   col1
    ----- -----------
    abc   111
    tgb   222(2 行受影响)
      

  6.   

    select * from tb1  where not  exists  (select  *  from  tb2 where tb1.col=tb2.col2 and tb1.col1=tb2.col3)