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

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);

Java Program!
原文地址:https://www.cnblogs.com/programb/p/12995884.html