如何删除表中的重复数据,只保留一条记录?


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