mysql数据库去除重复数据的方案
发布时间:2022-03-03 07:15:59 所属栏目:MySql教程 来源:互联网
导读:这篇文章主要介绍了mysql数据库去除重复数据的方法,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。 mysql数据库去除重复数据的方法:1、查询需要删除的记录,会保留一条记录;2、删除重复记
这篇文章主要介绍了mysql数据库去除重复数据的方法,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。 mysql数据库去除重复数据的方法:1、查询需要删除的记录,会保留一条记录;2、删除重复记录,只保留一条记录,代码为【delete a from test1 a, (...)as bid from test1 c where..】。 mysql数据库去除重复数据的方法: 1、查询需要删除的记录,会保留一条记录。 select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid 2、删除重复记录,只保留一条记录。注意,subject,RECEIVER 要索引,否则会很慢的。 delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid; 3、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 4、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1) 5、删除表中多余的重复记录(多个字段),只留有rowid最小的记录 delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) 看来想偷懒使用一句命令完成这个事好像不太显示,还是老老实实的分步处理吧,思路先建立复制一个临时表,然后对比临时表内的数据,删除主表里的数据 感谢你能够认真阅读完这篇文章,希望小编分享mysql数据库去除重复数据的方法内容对大家有帮助。 (编辑:湘西站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |