如何刪除表中的重復數據,只保留一條記錄?


1.通過創建臨時表

creat table tbl_tmp as select distinct * from tbl;

truncate table tbl;//清空表記錄

insert into tbl select * from tbl_tmp;//將臨時表中的數據插回來

 

2.利用rowid

delete from tbl where rowid in

(select a.rowid from tbl a, tbl b

where a.rowid>b.rowid

and a.col1=b.col1 and a.col2 = b.col2)

 

3.利用maxmin函數

delete from tbl a where rowid not in

(select max(b.rowid) from tbl b where a.col1=b.col1 and a.col2 = b.col2);

//這里max使用min也可以

 

delete from tbl where rowid not in

(select max(rowid) from tbl tgroup by t.col1, t.col2);


免責聲明!

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



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