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)
看来想偷懒使用一句命令完成这个事好像不太显示,还是老老实实的分步处理吧,思路先建立复制一个临时表,然后对比临时表内的数据,删除主表里的数据
alter table tablename add autoid int auto_increment not null; create table tmp select min(autoid) as autoid from tablename group by name,address; create table tmp2 select tablename.* from tablename,tmp where tablename.autoid = tmp.autoid; drop table tablename; rename table tmp2 to tablename;
更多相关免费学习推荐:mysql教程(视频)
以上就是mysql数据库如何去除重复数据的详细内容。
