有两个表tab1,tab2,两表结构完全相同.tab1和tab2的主键都为列s1,tab2的数据全是从tab1中抽出来的,现在要取出tab1中新增的数据,放到tab2中,示例: 
tab1的数据: 
s1 s2 s3
1  2  3 
1  2  null 
2  3  5 
tab2的数据: 
s1 s2 s3
1  2  null 
1  2  4 
现在想要得到的结果是: 
2  3  5 
用下面方法会得到含有null的记录
select * from tab1 where not exists( 
  select 1 from tab2 where tab1.t1 = tab2.t1 and tab1.t2=tab2.t2 and tab1.t3 = tab2.t3 

会得到两行记录1  2  null
2  3  5 如何解决这个问题,请大侠们指点···

解决方案 »

  1.   

    select * from tab1 where not exists( 
    select 1 from tab2 where tab1.t1 = tab2.t1 and tab1.t2=tab2.t2 and tab1.t3 = tab2.t3 

    and s3 is not null
      

  2.   

    select * from tab1 where not exists( 
      select 1 from tab2 where 
        (tab1.t1 = tab2.t1 or (tab1.t1 IS NULL and tab2.t1 IS NULL)) and 
        (tab1.t2 = tab2.t2 or (tab1.t2 IS NULL and tab2.t2 IS NULL)) and 
        (tab1.t3 = tab2.t3 or (tab1.t3 IS NULL and tab2.t3 IS NULL))

      

  3.   

    弱弱地问一句:为啥要让数据表中产生NULL了?
      

  4.   

    三楼兄台:弱弱地问一句:为啥要让数据表中产生NULL了?
              如果字段比较多,就有可能涉及到有空值的字段了二楼和三楼的兄台:感谢你们提供的思路,但是方法也不是我需要的,在之前已经试过两位兄台给的方法了,谢谢!
      

  5.   

    应该是3条记录都有吧,因为 NULL<>任何值
    select * from tt2 where not exists( 
      select 1 from tt3 where tt2.s1 = tt3.s1 and tt2.s2=tt3.s2 and COALESCE(tt2.s3,0) = 
    COALESCE(tt3.s3 ,0)
    )
      

  6.   

    or
    select * from tt2 where not exists( 
      select 1 from tt3 where tt2.s1 = tt3.s1 and tt2.s2=tt3.s2 and isnull(tt2.s3,0) = 
    isnull(tt3.s3 ,0) 
    )
      

  7.   

    To wwwwb
            如果字段比较多,有多个字段出现空值现象,这样解决不是一个最好的办法吧?
      

  8.   

    如果是这样,只有先替换字段,将NULL值修改为一个特定的值
    OR
    直接设置字段不能为NULL值
      

  9.   

      To wwwwb         在一些特定情况下是无法做到这两点滴,邹老大以前写过,一下找不到他的帖子了
      

  10.   


    SELECT * FROM tab1
    WHERE NOT EXISTS (SELECT * FROM tab2 WHERE tab1.t1 = tab2.t1 AND CHECKSUM(tab1.t2,tab1.t3) = CHECKSUM(tab2.t2,tab2.t3))