现在又两个表分别是A表( a ,b , c ,d , e ,f) B表(a,b,c,d)现在点击同步按钮,将A表的数据复制到B表,A,B两个同字段的,数据使用A表的数据(A,B当然通过主键关联),如果说A.a = B.a 剩下A表的数据要全部插入到B表!
需要用到存储过程
insert into B(a,b,c,d) select T.a,T.b,T.c,T.d from (select A.a,A.b,A.c,A.d from A left join B on A.a=B.a) T
这样插入不对,因为里面有相同的主键,如果主键相同就把A表的数据覆盖B表,就是B表更新,这样插入不对
update set B b=A.b,c=A.c,d=A.d from A where B.a=A.a insert into B(a,b,c,d) select A.a,A.b,A.c,A,d from A where A.a not in(select a from B)
for c in (select a,b,c,d from A left join B on A.a=B.a) loop update B set b=c.b,c=c.c,d=c.d where a=c.a; end loop;
insert into B(a,b,c,d) select A.a,A.b,A.c,A,d from A where A.a not in(select a from B);
可以用Merge 有就更新 没有就插入
update set B b=A.b,c=A.c,d=A.d from A where B.a=A.a insert into B(a,b,c,d) select A.a,A.b,A.c,A,d from A where A.a not in(select a from B)
for c in (select a,b,c,d from A left join B on A.a=B.a) loop update B set b=c.b,c=c.c,d=c.d where a=c.a; end loop;
insert into B(a,b,c,d) select A.a,A.b,A.c,A,d from A where A.a not in(select a from B);