mysql删除重复数据记录sql语句总结
删除重复记录的方法有很多种,这里我来总结了各种各样的删除重启记录的sql语句,如:删除id重复的数据,查找重复的,并且除掉最小的那个,删除重复记录,只保留一条记录和删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录等等.
我最常用的方法是,代码如下:
- //删除id重复的数据,适合id是手工主键
- deletepersonasafrompersonasa,
- (
- select*,min(id)frompersongroupbyidhavingcount(1)>1
- )asb
- wherea.id=b.id
- //查找重复的,并且除掉最小的那个
- deletetb_personasafromtb_personasa,
- (
- select*,min(id)fromtb_persongroupbynamehavingcount(1)>1
- )asb
- wherea.name=b.nameanda.id>b.id;
好了下面再总结一些:
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)
说了这么多了我们来个例子吧,例子如下:
- droptablet_user;
- createtablet_user(
- idint(5)notnullauto_increment,
- usernamevarchar(10),
- ageint(3),
- primarykey(id)
- );
- insertintot_user(username,age)values('aaa',20);
- insertintot_user(username,age)values('aaa',20);
- insertintot_user(username,age)values('bbb',20);
- insertintot_user(username,age)values('bbb',20);
- insertintot_user(username,age)values('ccc',20);
- insertintot_user(username,age)values('ccc',20);
- insertintot_user(username,age)values('ddd',20);
- insertintot_user(username,age)values('ddd',20);
- mysql>select*fromt_user;
- +----+----------+------+
- |id|username|age|
- +----+----------+------+
- |1|aaa|20|
- |2|aaa|20|
- |3|bbb|20|
- |4|bbb|20|
- |5|ccc|20|
- |6|ccc|20|
- |7|ddd|20|
- |8|ddd|20|
- +----+----------+------+
- mysql>deletet_userfromt_user,(selectidfromt_usergroupbyusernamehavingcount(*)>1)ast2wheret_user.id=t2.id;
- QueryOK,4rowsaffected(0.05sec)
- mysql>select*fromt_user;
- +----+----------+------+
- |id|username|age|
- +----+----------+------+
- |2|aaa|20|--phpfensi.com
- |4|bbb|20|
- |6|ccc|20|
- |8|ddd|20|
- +----+----------+------+
热门评论