北京SEO

mysql删除重复数据记录sql语句总结

2019/10/10/17:34:58  阅读:1799  来源:谷歌SEO算法  标签: Google

删除重复记录的方法有很多种,这里我来总结了各种各样的删除重启记录的sql语句,如:删除id重复的数据,查找重复的,并且除掉最小的那个,删除重复记录,只保留一条记录和删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录等等.

我最常用的方法是,代码如下:

  1. //删除id重复的数据,适合id是手工主键
  2. deletepersonasafrompersonasa,
  3. (
  4. select*,min(id)frompersongroupbyidhavingcount(1)>1
  5. )asb
  6. wherea.id=b.id
  7. //查找重复的,并且除掉最小的那个
  8. deletetb_personasafromtb_personasa,
  9. (
  10. select*,min(id)fromtb_persongroupbynamehavingcount(1)>1
  11. )asb
  12. 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)

说了这么多了我们来个例子吧,例子如下:

  1. droptablet_user;
  2. createtablet_user(
  3. idint(5)notnullauto_increment,
  4. usernamevarchar(10),
  5. ageint(3),
  6. primarykey(id)
  7. );
  8. insertintot_user(username,age)values('aaa',20);
  9. insertintot_user(username,age)values('aaa',20);
  10. insertintot_user(username,age)values('bbb',20);
  11. insertintot_user(username,age)values('bbb',20);
  12. insertintot_user(username,age)values('ccc',20);
  13. insertintot_user(username,age)values('ccc',20);
  14. insertintot_user(username,age)values('ddd',20);
  15. insertintot_user(username,age)values('ddd',20);
  16. mysql>select*fromt_user;
  17. +----+----------+------+
  18. |id|username|age|
  19. +----+----------+------+
  20. |1|aaa|20|
  21. |2|aaa|20|
  22. |3|bbb|20|
  23. |4|bbb|20|
  24. |5|ccc|20|
  25. |6|ccc|20|
  26. |7|ddd|20|
  27. |8|ddd|20|
  28. +----+----------+------+
  29. mysql>deletet_userfromt_user,(selectidfromt_usergroupbyusernamehavingcount(*)>1)ast2wheret_user.id=t2.id;
  30. QueryOK,4rowsaffected(0.05sec)
  31. mysql>select*fromt_user;
  32. +----+----------+------+
  33. |id|username|age|
  34. +----+----------+------+
  35. |2|aaa|20|--phpfensi.com
  36. |4|bbb|20|
  37. |6|ccc|20|
  38. |8|ddd|20|
  39. +----+----------+------+

广告内容

mysql删除重复数据记录sql语句总结 mysql删除重复数据记录sql语句总结 mysql删除重复数据记录sql语句总结

相关阅读

热门评论

昝辉Zac 昝辉Zac

Zac的SEO博客,坚持12年,优化成为生活。

总篇数171

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法