oracle删除重复记录行
[font=微软雅黑, 宋体]如有下表:
[font=微软雅黑, 宋体]create table persons (
[font=微软雅黑, 宋体]pid number,
[font=微软雅黑, 宋体]pname varchar2(20),
[font=微软雅黑, 宋体]cardid number,
[font=微软雅黑, 宋体]address varchar2(20));
[font=微软雅黑, 宋体][color=#328712]方法一:rowid
[font=微软雅黑, 宋体]-- 显示重复的行
[font=微软雅黑, 宋体]select * from persons p1
[font=微软雅黑, 宋体]where rowid<>
[font=微软雅黑, 宋体](select max(rowid) from persons p2 where p1.pid=p2.pid and p1.pname=p2.pname);
[font=微软雅黑, 宋体]-- 删除重复的行
[font=微软雅黑, 宋体]select * from persons p1
[font=微软雅黑, 宋体]where rowid<>
[font=微软雅黑, 宋体](select max(rowid) from persons p2 where p1.pid=p2.pid and p1.pname=p2.pname);
[font=微软雅黑, 宋体][color=#328712]方法二:group by
[font=微软雅黑, 宋体][color=#328712]
[font=微软雅黑, 宋体] -- 显示有重复的行
[font=微软雅黑, 宋体]
[font=微软雅黑, 宋体]select pid from persons group by pid having count(pid)>1;
[font=微软雅黑, 宋体] -- 删除重复的行
[font=微软雅黑, 宋体]
[font=微软雅黑, 宋体] delete from persons
[font=微软雅黑, 宋体]where pid in (select pid from persons group by pid having count(pid)>1);
[font=微软雅黑, 宋体]
[font=微软雅黑, 宋体] [font=微软雅黑, 宋体][size=15px][color=#AD0000]注: 此方法将删除有重复记录的所有行,不会留下一条刻录;
[font=微软雅黑, 宋体]
[font=微软雅黑, 宋体][color=#328712]方法三:distinct
[font=微软雅黑, 宋体]
[font=微软雅黑, 宋体] -- 取消重复行
[font=微软雅黑, 宋体]
[font=微软雅黑, 宋体]select distinct * from persons;
[font=微软雅黑, 宋体] -- 建表并复制“取消重复行的表中的数据”
[font=微软雅黑, 宋体] create table table_new as select distinct * from persons;
[font=微软雅黑, 宋体]-- 删除persons表中数据
[font=微软雅黑, 宋体] delete from persons;
[font=微软雅黑, 宋体]-- 把table_new表中没有重复行的数据插入到到persons表中
[font=微软雅黑, 宋体] insert into persons select * from table_new;
[font=微软雅黑, 宋体]