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

面试碰到一个MySQl的有趣的题目,如何从student表中删除重复名字的行,并保留最小id的记录?

很遗憾当时没有做出来,回家搜索了一番,发现利用子查询的可以很快解决。

1、删除表中多余的重复记录,重复记录是username判断,只留有id最小的记录

delete from studentwhere
username in ( select username from studentgroup by username having count(username)>1)
and id not in (select min(id) as id from studentgroup by username having count(username)>1 )

(上面这条语句在mysql中执行会报错:

执行报错:1093 - You can‘t specify target table ‘student‘ for update in FROM clause

原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。oracel和msserver都支持这种方式。

怎么规避这个问题?

再加一层封装,

delete from student where
username in (select username from ( select username from student group by username having count(username)>1) a)
and id not in ( select id from (select min(id) as id from student group by username having count(username)>1 ) b)

注意select min(id) 后面要有as id.

其实还有更简单的办法(针对单个字段):

delete from student where
id not in (select id from (select min(id) as id from student group by username) b);

拓展:

2、删除表中多余的重复记录(多个字段),只留有id最小的记录

delete from student a
where (a.username,a.seq) in (select username,seq from (select username,seq from a group by username,seq having count(*) > 1)  t1)
and id not in ( select id from (select min(id) from vitae group by username,seq having count(*)>1) t2)

参考文章:

https://blog.csdn.net/anya/article/details/6407280

原文地址:https://www.cnblogs.com/shihuibei/p/8809908.html

时间: 2024-10-15 02:07:43

MySQL删除重复数据只保留一条的相关文章

MySQL中删除重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT * FROM people WHERE peopleId IN ( SELECT peopleId FROM people GROUP BY peopleId HAVINGcount(peopleId) > 1 ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3

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

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

Oralce中SQL删除重复数据只保留一条(转)

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

MySQL删除重复记录只保留一条

删除表中重复记录,只保留一条: delete from 表名 where 字段ID in (select * from (select max(字段ID) from 表名 group by 重复的字段 having count(重复的字段) > 1) as b); 实例:2.当想要为某一个表建立一个唯一索引,由于表中有重复记录而无法进行时,需要删除重复记录.例表 dept id_no id_name 100 'AAA' 101 'BBB' 102 'CCC' 103 'DDD' 100 'EEE

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

1.表结构与数据: CREATE TABLE tablezzl( id int, name VARCHAR(255) ); 2.查询出重复的数据: SELECT name FROM tablezzl GROUP BY name HAVING COUNT(name)>1 3.查询出要保留的重复数据: SELECT MIN(id) ids FROM tablezzl GROUP BY NAME HAVING COUNT(name)>1 4.最终的SQL: DELETE FROM tablezzl

Mysql 删除重复数据只保留id最小的

DELETE FROM 表 WHERE id NOT IN ( SELECT id FROM ( SELECT min(b.id) AS id FROM 表 b GROUP BY b.重复字段 ) b )

oracle 删除掉重复数据只保留一条

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

mysql删除重复数据,保留最新的那一条

因为数据库没键外键,在关联查询的时候,会碰到查询条数多余数据库实际条数,这因为关联字段在表中有重复值而导致的. 解决方案: 1.数据库脚本删除重复数据,保留最新的一条 2.对关联字段增加唯一约束 例如: 以下表,部门表的部门编号出现了重复. 首先判断是不是重复 1 select count(*) from department d 2 3 select count(*) from ( select distinct dept_code from department ) 看以上查出来的数量是不是

【转】SQL删除重复记录,只保留其中一条

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