两个表:表A,B和表C,具有相同的字段结构,其中有字段z1,z2共同做主键
现在根据主键找出表A有而表B没有的记录和表B有表A没有的记录,并插入到表C

解决方案 »

  1.   

    首先,主建肯定不可能有两个的,首引是可以的
    ----------------------------------------------Insert into C
    Values (Select A.col1,A.Col2,... 
    From A,B
    Where (A.z1=B.z1 and A.Z2 <> B.Z2)  or (A.z1<>B.z1 and A.Z2 = B.Z2) or(A.z1<>B.z1 and A.Z2 <> B.Z2));Insert into C
    Values (Select B.col1,B.Col2,...
    From A,B
    Where (A.z1=B.z1 and A.Z2 <> B.Z2)  or (A.z1<>B.z1 and A.Z2 = B.Z2) or(A.z1<>B.z1 and A.Z2 <> B.Z2));
      

  2.   

    首先跟你说,一个表中是不可能有两个主键的,你的说法有问题,有两个索引差不多
    Insert into C
    Values (fields)
    select (A.z1=B.z1 and A.Z2 <> B.Z2)  or (A.z1<>B.z1 and A.Z2 = B.Z2) or
    (A.z1<>B.z1 and A.Z2 <> B.Z2))
    From A,B
      

  3.   

    insert 表C select * from 表A where not exists(select 1 from 表B where z1=表A.z1 and z2=表A.z2) union all select * from 表B where not exists(select 1 from 表A where z1=表B.z1 and z2=表B.z2)