首页 > 数据库 > 数据库开发技术 > MySQL如何查找并删除重复记录
如果觉得文章有用,欢迎关注❤️、点赞👍、推
drop table if exists people;
create table people (
id int auto_increment primary key,
name varchar(50) not null,
email varchar(100) not null
);
insert into people(name, email)
values ('张三', 'zhangsan@test.com'),
('李四', 'lisi@test.com'),
('王五', 'wangwu@test.com'),
('李斯', 'lisi@test.com'),
('王五', 'wangwu@test.com'),
('王五', 'wangwu@test.com');
select * from people;
id|name |email |
--|------|-----------------|
1|张三 |zhangsan@test.com|
2|李四 |lisi@test.com |
3|王五 |wangwu@test.com |
4|李斯 |lisi@test.com |
5|王五 |wangwu@test.com |
6|王五 |wangwu@test.com |
alter table people add constraint uk_people_email unique key (email);
ERROR 1062 (23000): Duplicate entry 'wangwu@test.com' for key 'people.uk_people_email'
select email, count(email)
from people
group by email
having count(email) > 1;
email |count(email)|
---------------|------------|
lisi@test.com | 2|
wangwu@test.com| 3|
select *
from people
where email in (
select email
from people
group by email
having count(email) > 1)
order by email;
id|name |email |
--|------|---------------|
2|李四 |lisi@test.com |
4|李斯 |lisi@test.com |
3|王五 |wangwu@test.com|
5|王五 |wangwu@test.com|
6|王五 |wangwu@test.com|
select p.*
from people p
join (
select email
from people
group by email
having count(email) > 1
) d on p.email = d.email
order by email;
id|name |email |
--|------|---------------|
2|李四 |lisi@test.com |
4|李斯 |lisi@test.com |
3|王五 |wangwu@test.com|
5|王五 |wangwu@test.com|
6|王五 |wangwu@test.com|
select distinct p.*
from people p
join people d on p.email = d.email
where p.id <> d.id
order by p.email;
id|name |email |
--|------|---------------|
4|李斯 |lisi@test.com |
2|李四 |lisi@test.com |
6|王五 |wangwu@test.com|
5|王五 |wangwu@test.com|
3|王五 |wangwu@test.com|
select *
from people
where (name, email) in (
select name, email
from people
group by name, email
having count(1) > 1)
order by email;
id|name |email |
--|------|---------------|
3|王五 |wangwu@test.com|
5|王五 |wangwu@test.com|
6|王五 |wangwu@test.com|
select distinct p.*
from people p
join people d on p.name = d.name and p.email = d.email
where p.id <> d.id
order by email;
id|name |email |
--|------|---------------|
6|王五 |wangwu@test.com|
5|王五 |wangwu@test.com|
3|王五 |wangwu@test.com|
delete p
from people p
join people d on p.email = d.email and p.id < d.id;
select * from people;
id|name |email |
--|------|-----------------|
1|张三 |zhangsan@test.com|
4|李斯 |lisi@test.com |
6|王五 |wangwu@test.com |
delete
from people
where id not in (
select max(id)
from people
group by email
);
-- 创建中间表
create table people_temp like people;
-- 复制需要保留的数据行
insert into people_temp(id, name, email)
select id, name, email
from people
where id in (
select max(id)
from people
group by email
);
--删除原表
drop table people;
-- 将中间表重命名为原表
alter table people_temp rename to people;
select id, name, email,
row_number() over (partition by email order by id) as row_num
from people;
id|name |email |row_num|
--|------|-----------------|-------|
2|李四 |lisi@test.com | 1|
4|李斯 |lisi@test.com | 2|
3|王五 |wangwu@test.com | 1|
5|王五 |wangwu@test.com | 2|
6|王五 |wangwu@test.com | 3|
1|张三 |zhangsan@test.com| 1|
delete
from people
where id in (
select id
from (
select id,
row_number() over (partition by email order by id desc) as row_num
from people) d
where row_num > 1);
原文作者:SQL编程思想;如有侵权,请联系管理员删除。