实现把表A中的所有记录移到表B中,对于B表没有对应的记录就insert,如果有对应记录则update,
例如表A中有一条记录111,表B中没有,则直接insert进表B,如果有就update表B中记录为111的记录。A表主键和B表主键是一样的。我的实现是:
1. update 表B中有的记录。例如
(1)查询出A表中的所有的记录
select * from A
(2)循环查询出来的记录,来updateB表
for (record)
update B set .... where B.主键 = 条件
2. 插入记录到B表 (之前更新过的记录不插入)
insert into B(....) select ... from A where B.主键 not in (select A.主键 from A);
大家有没有更好的实现?
例如表A中有一条记录111,表B中没有,则直接insert进表B,如果有就update表B中记录为111的记录。A表主键和B表主键是一样的。我的实现是:
1. update 表B中有的记录。例如
(1)查询出A表中的所有的记录
select * from A
(2)循环查询出来的记录,来updateB表
for (record)
update B set .... where B.主键 = 条件
2. 插入记录到B表 (之前更新过的记录不插入)
insert into B(....) select ... from A where B.主键 not in (select A.主键 from A);
大家有没有更好的实现?
在存储过程里边逐条移动 每次的俩表操作之间用事务机制包上,这样就更安全了,如果一条出错吧id或者其他的表示符记载到变量中最后输出,这样既不影响整体的速度,又能准确的指导那些数据失算了,再重新导入一次就基本上差不多了,当然我说的知识方法跟思虑,自己百度下存储过程的写法 很简单,这个是效率最快的sql操作
以上的逻辑是要使用java server 类调用 dao 类实现。
1st select a.id from A a where exits (select 1 from B b where a.id=b.id)
根据查询的id,update table B2nd ececute insert: insert into B(c1,c2,c3) value
(select c.c1,c.c2,c.c3 from A c where exists
(select 1 from (select a.id from A a where not exits
(select 1 from B b where a.id=b.id)
) T where T.id=c.id
)
)
ls,对我解决方案的第一步有改进,第一步只需要找出B表和A表中都有的记录,然后update B表,
第二步,基本一样。。
第一步 先删除表B中的全部数据
第二步 再向B表中插入所有A表中的数据