rowid去重复数据

⑴ 通过创建临时表 

可以把数据先导入到一个临时表中,然后删除原表的数据,再把数据导回原表,SQL语句如下: 

SQL>create table stu_tmp as select distinct* from stu; SQL>truncate table sut;        

//清空表记录 

SQL>insert into stu select * from stu_tmp;    //将临时表中的数据添加回原表  

这种方法可以实现需求,但是很明显,对于一个千万级记录的表,这种方法很慢,在生产系统中,这会给系统带来很大的开销,不可行。  

⑵ 利用rowid结合max或min函数 

使用rowid快速唯一确定重复行结合max或min函数来实现删除重复行。 

SQL>delete from stu a where rowid not in (select max(b.rowid) from stu b where a.no=b.no and a.name = b.name and a.sex = b.sex);      //这里max使用min也可以 或者用下面的语句 

SQL>delete from stu a where rowid < (select max(b.rowid) from stu b where a.no=b.no and a.name = b.name and a.sex = b.sex); //这里如果把max换成min的话,前面的where子句中需要把"<"改为">" 

跟上面的方法思路基本是一样的,不过使用了group by,减少了显性的比较条件,提高效率。 

SQL>delete from stu where rowid not in (select max(rowid) from stu t group by t.no, t.name, t.sex );

猜你喜欢

转载自hongwei3344661.iteye.com/blog/2351352
今日推荐