Oracle数据库删除表中的重复数据,只保留其中的一条,以两个字段为例,提供两种方法

①、直接delete重复的数据

delete from table_name t1 
where (t1.col1, t1.col2) in (select col1, col2 from table_name group by col1, col2 having count(*) > 1)
and  t1.rowid in (select min(rowid) from table_name group by col1, col2 having count(*) > 1);

一直执行到没有数据变化位置。

②、查询出所有不重复的数据,入到一张中间表中,然后把原表的数据清空,最后把中间表的数据入到原表中

--数据入到中间表中
insert into table_mid_name 
select t1.* from table_name t1 
where t1.rowid in (select min(rowid) from table_name group by col1, col2 ) ;
--清除原表数据
truncate table table_name ;
--数据入到原表中
insert into table_name select * from table_mid_name ;
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐