SQL删除重复数据,仅保留(最新/有效的)一条数据

news/2025/1/15 21:46:57/

表结构

CREATE TABLE `people` (`id` int(11) NOT NULL AUTO_INCREMENT,`peopleName` varchar(2000) DEFAULT NULL COMMENT 'peopleName',`status` tinyint(1) DEFAULT NULL COMMENT '0无效 1有效',`seq` int(5) NOT NULL DEFAULT '1' COMMENT '记录值',`peopleId` tinyint(4) DEFAULT '0' COMMENT 'peopleId',`create_time` datetime DEFAULT CURRENT_TIMESTAMP,`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1133 DEFAULT CHARSET=utf8 COMMENT='people测试表';

删除重复值

  1. 查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
  1. 删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有peopleId最小的记录
delete from people
where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1)
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1)
  1. 查找表中多余的重复记录(多个字段)
select * from people a
where (a.peopleId,a.seq) in (select peopleId,seq from people group by peopleId,seq having count(*) > 1)
  1. 删除表中多余的重复记录(多个字段),只留有id最小的记录
delete from people a
where (a.peopleId,a.seq) in (select peopleId,seq from people group by peopleId,seq having count(*) > 1)
and id not in (select min(id) from people group by peopleId,seq having count(*)>1)
  1. 查找表中多余的重复记录(多个字段),不包含id最小的记录
select * from people a
where (a.peopleId,a.seq) in (select peopleId,seq from people group by peopleId,seq having count(*) > 1)
and id not in (select min(id) from people group by peopleId,seq having count(*)>1)   

记录使用过程中出现的错误

在使用mysql5.7时,发现存在如下的报错

You can't specify target table for update in FROM clause
含义:不能在同一表中查询的数据作为同一表的更新数据。

解决方法

-- 以以下的SQL举例
delete from people
where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1)
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1)-- 修改为
delete from people
where   peopleName inselect a.peopleName from (select peopleName    from people group by peopleName      having count(peopleName) > 1) a) 
and   peopleId not in (select b.peopleName from (select min(peopleId) from people group by peopleName     having count(peopleName)>1) b)

参考
[1]https://blog.csdn.net/u012767761/article/details/84997962
[2]https://blog.csdn.net/anya/article/details/6407280


http://www.ppmy.cn/news/734769.html

相关文章

SQL删除重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from …

利用sql语句删除重复数据

记录原因: 昨天遇到一个问题:需要写一个sql语句删除重复数据。当时解决的办法,是先查询出重复的数据id,然后再单独写删除语句进行删除。今天想了想应该还是有其他办法解决的,研究了下可以通过一条语句就能解决。记录下顺便做下对比: 单独写…

sql 删除重复数据 只留一条

目录 常用 其他 mysql 常用 oracle中,借助rowid,来留下一条 delete from t1where (f1,f2,f3) in (select f1,f2,f3 from t1 group by f1,f2,f3 having count(*) > 1)and rowid not in (select min(rowid) from t1 group by f1,f2,f3 having cou…

sql删除重复数据只保留一条的操作方法

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2、删除表中多余的重复记录,重…

elixir语言并发编程

文章目录 spwan进程通信spawn_link erlang教程: 基础入门🔥 编程基础🔥 数据结构🔥 递归🔥 并发编程 elixir教程: 基础入门🔥 编程基础🔥 模式匹配🔥 递归枚举 …

SQL删除重复数据

例: idnamelogin_date1张三2023-04-292张三2023-04-303李四2023-04-304王五2023-04-305张三2023-04-306李四2023-04-307王五2023-05-018张三2023-05-019李四2023-05-0110王五2023-05-01 因为表的主键(id)是唯一的,所以只需要找到…

web服务端接收多用户并发上传同一文件,保证文件副本只存在一份(附go语言实现)

背景 对于一个文件服务器来说,对于同一文件,应该只保存一份在服务器上。基于这个原则,引发出本篇内容。 本篇仅阐述文件服务器在同一时间接收同一文件的并发问题,这种对于小体量的服务来说并不常见,但是最好还是要留…

使用jumpserver堡垒机管理王者荣耀服务器

堡垒机概述: 堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的***和破坏,而运用各种技术手段实时收集和监控网络环境中每一个组成部分的系统状态、安全事件、网络活动,以便集中报警、及时…