Oracle刪除表中的重復數據


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 ;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM