有两个表TableA、TableB.
两个表的结构相同。结构如下。
Field1     Field2     Field3     Field4     Field5
其中  Field1+Field2  为主键。现在要求将TableB的记录中在TableA中主键不存在的记录插入到TableA中。比如。
TableA如下。
Field1     Field2     Field3     Field4     Field5
   A         A          B          B          B
   A         B          C          C          C
TableB如下。
Field1     Field2     Field3     Field4     Field5
   A         A          B          B          B
   C         D          1          2          3
运算后TableA结果如下。
Field1     Field2     Field3     Field4     Field5
  A         A          B          B          B
  A         B          C          C          C
  C         D          1          2          3

解决方案 »

  1.   

    insert into tablea
    select * from tableb where not exists(select * from tableb where tableb.field1=tablea.field1 and tableb.field2=tablea.field2)
      

  2.   

    insert tableA
      select * from TableB b
      left join tableA a on a.FieldA=b.FieldA and a.FieldB=b.FieldB
      where a.FieldA is null
      

  3.   

    insert into TableA select TableB.* from TableB  full join TableA  on 
    TableA.Field1 =TableB.Field1 and TableA.Field2 =TableB.Field2
    where TableA.Field1 is null
      

  4.   

    insert into TableA(Field1,Field2,Field3,Field4,Field5)
    select Field1,Field2,Field3,Field4,Field5 from TableB
    where Field1+Field2 not in (select Field1+Field2 from TableA)
      

  5.   

    insert into tableA
    select * from tableB B 
    where not exists(select 1 from tableA A where A.Field1=B.Field1 and A.Field2=B.Field2)