数据库去重-v1.0版

select catid from supe_categories where name='金融';
create table multmp select * from supe_spaceitems items,supe_categories cate where cate.name='金融' and items.catid=cate.catid;
create table tmpa as select itemid from multmp,(select subject from multmp group by subject having count(itemid)>1) t1 where multmp.subject=t1.subject;
create table tmpb as select min(itemid) as itemid  from multmp group by subject having count(itemid)>1;
create table tmptmp as select  t.itemid  from (select multmp.itemid from multmp,tmpa where multmp.itemid=tmpa.itemid) t,tmpb where t.itemid<>tmpb.itemid group by t.itemid;

delete from supe_spacenews using supe_spacenews,tmptmp where supe_spacenews.itemid=tmptmp.itemid;
delete from multmp using multmp,tmptmp where multmp.itemid=tmptmp.itemid;
drop table tmpa;
drop table tmpb;
drop table tmptmp;

delete from supe_spaceitems using supe_spaceitems,supe_categories where supe_categories.name='金融'  and supe_categories.catid=supe_spaceitems.catid;
insert into supe_spaceitems select * from multmp;
drop table multmp;

更新:去除所有的in操作,加速sql 
遗留问题:使用临时表,这样不允许多方同时操作数据库。仍需改进,可能所有sql需要重新操作。

你可能感兴趣的:(sql,金融)