主页 > 知识库 > MySQL 数据查重、去重的实现语句

MySQL 数据查重、去重的实现语句

热门标签:许昌外呼增值业务线路 广东400企业电话申请流程 咸阳防封电销卡 临沂做地图标注 申请400电话电话价格 宜宾全自动外呼系统厂家 新乡智能外呼系统好处 石家庄400电话办理公司 地图标注客户付款

有一个表user,字段分别有id、nick_name、password、email、phone。

一、单字段(nick_name)

查出所有有重复记录的所有记录

select * from user where nick_name in (select nick_name from user group by nick_name having count(nick_name)>1);

查出有重复记录的各个记录组中id最大的记录

select * from user where id in (select max(id) from user group by nick_name having count(nick_name)>1);

查出多余的记录,不查出id最小的记录

select * from user where nick_name in (select nick_name from user group by nick_name having count(nick_name)>1) and id not in (select min(id) from user group by nick_name having count(nick_name)>1);

删除多余的重复记录,只保留id最小的记录

delete from user where nick_name in (select nick_name from (select nick_name from user group by nick_name having count(nick_name)>1) as tmp1) and id not in (select id from (select min(id) from user group by nick_name having count(nick_name)>1) as tmp2);

二、多字段(nick_name,password)

查出所有有重复记录的记录

select * from user where (nick_name,password) in (select nick_name,password from user group by nick_name,password where having count(nick_name)>1);
 

查出有重复记录的各个记录组中id最大的记录

select * from user where id in (select max(id) from user group by nick_name,password where having count(nick_name)>1);

查出各个重复记录组中多余的记录数据,不查出id最小的一条

select * from user where (nick_name,password) in (select nick_name,password from user group by nick_name,password having count(nick_name)>1) and id not in (select min(id) from user group by nick_name,password having count(nick_name)>1);

删除多余的重复记录,只保留id最小的记录

delete from user where (nick_name,password) in (select nick_name,password from (select nick_name,password from user group by nick_name,password having count(nick_name)>1) as tmp1) and id not in (select id from (select min(id) id from user group by nick_name,password having count(nick_name)>1) as tmp2);

以上就是MySQL 数据查重、去重的实现语句的详细内容,更多关于MySQL 数据查重、去重的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
  • MySQL使用show status查看MySQL服务器状态信息
  • Mysql中 show table status 获取表信息的方法
  • mysql建表常用的sql语句汇总
  • 记一次MySQL更新语句update的踩坑
  • Navicat Premium操作MySQL数据库(执行sql语句)
  • MySQL两种删除用户语句的区别(delete user和drop user)
  • MySQL 数据库 like 语句通配符模糊查询小结
  • mysql中写判断语句的方法总结
  • MySQL 常用的拼接语句汇总
  • MySQL SHOW STATUS语句的使用

标签:阜新 合肥 鹰潭 镇江 贵州 台湾 日照 北京

巨人网络通讯声明:本文标题《MySQL 数据查重、去重的实现语句》,本文关键词  MySQL,数,据查,重,去,重的,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《MySQL 数据查重、去重的实现语句》相关的同类信息!
  • 本页收集关于MySQL 数据查重、去重的实现语句的相关信息资讯供网民参考!
  • 推荐文章