mysql数据库怎样去掉反复数据
发布时间:09/01 来源:未知 浏览:
关键词:
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;