首页 > 其他 > 详细

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

时间:2020-05-07 20:46:24      阅读:88      评论:0      收藏:0      [点我收藏+]

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

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

原文:https://www.cnblogs.com/programb/p/12845126.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!