关于mysql数据库两个表数据对比问题?

2024-11-08 22:47:42
推荐回答(2个)
回答(1):

不建议图省事,老老实实写三条比较好。
insert into T2 (A,B,C) select T1.A,T1.B,T1.C from T1 left join T2 on T1.A = T2.A and ( T1.B = T2.B or T1.C = T2.C );
insert into T2 (A,B,C) select T1.A,T1.B,T1.C from T1 left join T2 on T1.B = T2.B and ( T1.A = T2.A or T1.C = T2.C );
insert into T2 (A,B,C) select T1.A,T1.B,T1.C from T1 left join T2 on T1.C = T2.C and ( T1.B = T2.B or T1.A = T2.A );

回答(2):

insert into T2(A,B,C) select A,B,C from T1 where concat(A,B,C) not in (select concat(A,B,C) from T2)
注意:以上语句不能处理A,B,C中有null值的情况。