首页 > 代码库 > oracle__删除重复记录__三种方法及总结(转载百度文库)

oracle__删除重复记录__三种方法及总结(转载百度文库)

http://wenku.baidu.com/link?url=RIENeGUK4sjxe21_RBYLYHR9tbUUCmOZQRR0mIjldXLYwRAt4khDtLQD9dFyd3rz3s_HWLvG2oErTw8sJUb1R2QLQqSZaBO3xLA8tu2qd9q

 

--方法1:rowid

--显示重复的行 select * from persons p1 where rowid<> (select max(rowid) from persons p2 where p1.pid=p2.pid);

说明:

如果,有多个字段相同,则添加即可,如: select * from persons p1 where rowid<> (select max(rowid) from persons p2 where p1.pid=p2.pid and p1.pname=p2.pname);

--删除重复的行 delete from persons p1 where rowid<> (select max(rowid) from persons p2 where p1.pid=p2.pid);

总结:

A:每张表的记录可能相同,但是每张表的记录的ROWID是唯一的;

B:做题思路

   a:显示相同字段的最大ROWID       b:留下相同字段的最大ROWID所在的记录,其它的记录删除;

--方法二:group by

按照PID分组,显示pid数量大于1的记录

select pid from persons group by pid having count(pid)>1;

删除重复行

delete from persons where pid in (select pid from persons group by pid having count(pid)>1);

此方法将删除有重复记录的所有行,不会留下一条刻录;

--方法三:distinct

取消重复行 select distinct * from persons;

建表并复制“取消重复行的表中的数据” create table table_new as select distinct * from persons;

删除persons表中数据 delete from persons;

把table_new表中没有重复行的数据插入到到persons表中 insert into persons select * from table_new;

总结:

使用中间表导入导出数据来删除重复行;