首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

ORACLE剔除重复记录方法

2012-07-30 
ORACLE删除重复记录方法几个删除重复记录的SQL语句1.用rowid方法2.用group by方法?现有一个人员表persons,

ORACLE删除重复记录方法

几个删除重复记录的SQL语句
1.用rowid方法
2.用group by方法

?

现有一个人员表persons,有三个成员:ID,CARDID,PNAME


1.用rowid方法

select *  from persons a where rowid != (select max(rowid)                   from persons b                  where a.id = b.id                    and a.cardid = b.cardid                    and a.pname = b.pname)

?

?删除:

delete from persons a where rowid != (select max(rowid)                   from persons b                  where a.id = b.id                    and a.cardid = b.cardid                    and a.pname = b.pname)--无法处理为NULL情况

?


2.用group by方法

查找:

select t.*  from base.persons t group by t.id, t.cardid, t.pnamehaving count(*) > 1;

?

delete from base.persons x where (x.id, x.cardid, x.pname) in       (select t.id, t.cardid, t.pname          from base.persons t         group by t.id, t.cardid, t.pname        having count(*) > 1);--但是这种对id,cardid两列相同,pname为空的处理不了,因为in无法处理NULL

?

热点排行