时间:2021-05-24
有一个表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 数据查重、去重的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
mysql查询的控制语句字段去重**关键字:distinct**语法:selectdistinct字段名from表名;案例:对部门进行去重Selectdisti
本文实例讲述了MySQL重定位数据目录的实现方法。分享给大家供大家参考。具体方法如下:MySQL允许重定位数据目录或其中的成员,这里就来介绍重定位MySQL数据
drop_duplicates为我们提供了数据去重的方法,那怎么得到哪些数据有重复呢?实现步骤:1、采用drop_duplicates对数据去两次重,一
在项目中我们常会对数据进行去重处理,有时候会用in或者EXISTS函数。或者通过groupby也是可以实现查重不过Postgresql还有自带去重函数:dist
本文实例讲述了JS实现字符串去重及数组去重的方法。分享给大家供大家参考,具体如下:www.jb51.netjs数组、字符串去重functionq