我有两张表A,B,A表的数据有很多重复的人,如下
pid name phone
P421 小王 13881231
P187 小李 13498798
J986 小张 15812931
J823 小王 13881231
P283 小张 15812931
J180 小李 13498798
......还有一张关联表B
linkid pid orgid
1 P421 o122
2 P187 o124
3 J986 o123
4 J180 o231
5 283 o871
6 J823 o281
......我想要删除A表中的所有重复的人的数据,以P开头的为准,同时替换B中,被删除的数据的同名的pid。
请问哪位高手,帮我写个sql,批量修改,数据量实在有点大。
pid name phone
P421 小王 13881231
P187 小李 13498798
J986 小张 15812931
J823 小王 13881231
P283 小张 15812931
J180 小李 13498798
......还有一张关联表B
linkid pid orgid
1 P421 o122
2 P187 o124
3 J986 o123
4 J180 o231
5 283 o871
6 J823 o281
......我想要删除A表中的所有重复的人的数据,以P开头的为准,同时替换B中,被删除的数据的同名的pid。
请问哪位高手,帮我写个sql,批量修改,数据量实在有点大。
select * from A
order by pid desc,name;
v_cur cur%rowtype;
v_pre_pid A.pid%type:="";
v_pre_name A.name%type:="";begin open cur;
loop
fetch cur into v_cur;
exit when cur%notfound; if v_cur.name = v_pre_name then
update B set pid = v_pre_pid where pid = v_cur.pid;
delete A where pid = v_cur.pid;
else
v_pre_pid:=v_cur.pid;
v_pre_name:=v_cur.name;
end if;
end loop;commit;end;
删除大概可以这样吧??
还得你自己写吧,别人没有你的数据,写出来也可能会出错,自己可以联系下。
更新B表。没明白啥意思
delete from b where exists(select pid from a where exists(select 1 from (select min(pid) pid,name from a group by name) z where z.name=a.name and z.pid<a.pid) and a.pid=b.pid );再删除a中重复的数据(重复的保留一条)delete from a where exists(select 1 from (select min(pid) pid,name from a group by name) z where z.name=a.name and z.pid<a.pid);试试
先删除b中的数据delete from b where exists(select pid from a where exists(select 1 from (select min(rowid)
rowid,name from a group by name) z where z.name=a.name and z.rowid<a.rowid) and a.pid=b.pid ); 再删除a中重复的数据(重复的保留一条)delete from a where exists(select 1 from (select min(rowid) rowid,name from a group by name) z
where z.name=a.name and z.rowid<a.rowid);