主頁 > 知識(shí)庫 > MySQL 如何查找并刪除重復(fù)記錄的實(shí)現(xiàn)

MySQL 如何查找并刪除重復(fù)記錄的實(shí)現(xiàn)

熱門標(biāo)簽:地方門戶網(wǎng)站 服務(wù)外包 鐵路電話系統(tǒng) 網(wǎng)站排名優(yōu)化 呼叫中心市場需求 AI電銷 Linux服務(wù)器 百度競價(jià)排名

大家好,我是只談技術(shù)不剪發(fā)的 Tony 老師。由于一些歷史原因或者誤操作,可能會(huì)導(dǎo)致數(shù)據(jù)表中存在重復(fù)的記錄;今天我們就來談?wù)勅绾尾檎?MySQL 表中的重復(fù)數(shù)據(jù)以及如何刪除這些重復(fù)的記錄。

創(chuàng)建示例表

首先創(chuàng)建一個(gè)示例表 people 并生成一些數(shù)據(jù):

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 |

其中,2 和 4 的 email 字段存在重復(fù)數(shù)據(jù);3、5 和 6 的 name 和 email 字段存在重復(fù)數(shù)據(jù)。

此時(shí),如果我們想要為 email 創(chuàng)建一個(gè)唯一約束,將會(huì)返回錯(cuò)誤:

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'

顯然,我們必須找出并刪除 email 字段中的重復(fù)記錄才能創(chuàng)建唯一約束。

查找單個(gè)字段中的重復(fù)數(shù)據(jù)

如果想要找出 email 重復(fù)的數(shù)據(jù),可以基于該字段進(jìn)行分組統(tǒng)計(jì),并且返回行數(shù)大于 1 的分組:

select email, count(email)
from people
group by email
having count(email) > 1;
email   |count(email)|
---------------|------------|
lisi@test.com |   2|
wangwu@test.com|   3|

查詢結(jié)果顯示有兩個(gè)郵箱地址存在重復(fù)情況。如果想要查看完整的重復(fù)數(shù)據(jù),可以使用子查詢或者連接查詢:

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|

另一種查找重復(fù)記錄的方法就是直接使用自連接查詢和 distinct 操作符,例如:

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|

注意,不能省略 distinct,否則會(huì)某些數(shù)據(jù)(3、5、6)會(huì)返回多次。

查找多個(gè)字段中的重復(fù)數(shù)據(jù)

如果我們想要找出 name 和 email 字段都重復(fù)的數(shù)據(jù),實(shí)現(xiàn)方式也類似:

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|

只有當(dāng) name 和 email 都相同時(shí)才是重復(fù)數(shù)據(jù),所以 2 和 4 不是重復(fù)記錄。

刪除重復(fù)數(shù)據(jù)

找出重復(fù)數(shù)據(jù)之后,需要解決的就是如何刪除了,通常我們需要保留其中的一條記錄。

使用 DELETE FROM 刪除重復(fù)數(shù)據(jù)

假如我們想要?jiǎng)h除 email 重復(fù)的記錄,只保留其中一條,可以使用 DELETE FROM 語句實(shí)現(xiàn):

delete p
from people p
join people d on p.email = d.email and p.id  d.id;

delete 語句通過連接找出需要?jiǎng)h除的記錄,以上示例保留了重復(fù)數(shù)據(jù)中的最大 id 對(duì)應(yīng)的數(shù)據(jù)行。再次查詢 people 表:

select * from people;
id|name |email   |
--|------|-----------------|
 1|張三 |zhangsan@test.com|
 4|李斯 |lisi@test.com |
 6|王五 |wangwu@test.com |

想一想,如果想要保留重復(fù)數(shù)據(jù)中 id 最小的數(shù)據(jù)應(yīng)該怎么實(shí)現(xiàn)呢?

利用子查詢刪除重復(fù)數(shù)據(jù)

通過子查詢可以找出需要保留的數(shù)據(jù),然后刪除其他的數(shù)據(jù):

delete
from people
where id not in (
  select max(id)
  from people
  group by email
  );

在執(zhí)行上面的語句之前,記得重新創(chuàng)建 people 表并生成測試數(shù)據(jù)。

通過中間表刪除重復(fù)數(shù)據(jù)

通過使用中間表也可以實(shí)現(xiàn)重復(fù)記錄的刪除,例如:

-- 創(chuàng)建中間表
create table people_temp like people;

-- 復(fù)制需要保留的數(shù)據(jù)行
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;

在執(zhí)行上面的語句之前,記得重新創(chuàng)建 people 表并生成測試數(shù)據(jù)。

這種方式需要注意的一個(gè)問題就是 create table … like 語句不會(huì)復(fù)制原表上的外鍵約束,需要手動(dòng)添加。

利用窗口函數(shù)刪除重復(fù)數(shù)據(jù)

ROW_NUMBER() 是 MySQL 8.0 中新增的窗口函數(shù),可以用于將數(shù)據(jù)進(jìn)行分組,然后為每一條數(shù)據(jù)分配一個(gè)唯一的數(shù)字編號(hào)。例如:

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|

以上語句基于 email 分組(partition by email),同時(shí)按照 id 進(jìn)行排序(order by id),然后為每個(gè)組內(nèi)的數(shù)據(jù)分配一個(gè)編號(hào);如果編號(hào)大于 1 就意味著存在重復(fù)的數(shù)據(jù)。

📝除了 ROW_NUMBER() 之外,RANK() 或者 DENSE_RANK() 函數(shù)也可以實(shí)現(xiàn)以上功能。關(guān)于窗口函數(shù)的介紹和使用案例,可以參考這篇文章。

基于該查詢結(jié)果可以刪除重復(fù)的記錄:

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);

在執(zhí)行上面的語句之前,記得重新創(chuàng)建 people 表并生成測試數(shù)據(jù)。

基于多個(gè)字段的重復(fù)數(shù)據(jù)刪除方法和單個(gè)字段非常類似,大家可以自行嘗試,也歡迎留言討論!

總結(jié)

本文介紹了如何在 MySQL 中查找并刪除重復(fù)記錄,包括使用 GROUP BY 分組、子查詢或者連接查詢等方法查找單個(gè)字段或者多個(gè)字段中的重復(fù)數(shù)據(jù),以及使用 DELETE FROM 語句、子查詢、中間表和窗口函數(shù)等方法實(shí)現(xiàn)重復(fù)數(shù)據(jù)的刪除。更多相關(guān)MySQL 查找并刪除重復(fù)記錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • mysql判斷表是否存在然后批量刪除的操作
  • MySQL刪除數(shù)據(jù),表文件大小依然沒變的原因
  • MySQL刪除表的三種方式(小結(jié))
  • MySQL 快速刪除大量數(shù)據(jù)(千萬級(jí)別)的幾種實(shí)踐方案詳解
  • 刪除mysql服務(wù)的具體方法
  • MySQL 處理重復(fù)數(shù)據(jù)的方法(防止、刪除)
  • MySQL對(duì)數(shù)據(jù)庫操作(創(chuàng)建、選擇、刪除)
  • Window下如何恢復(fù)被刪除的Mysql8.0.17 Root賬戶及密碼
  • Linux下徹底刪除Mysql 8.0服務(wù)的方法
  • MySQL刪除表操作實(shí)現(xiàn)(delete、truncate、drop的區(qū)別)
  • MySQL刪除了記錄不生效的原因排查

標(biāo)簽:仙桃 蘭州 衡水 銅川 崇左 湘潭 湖南 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL 如何查找并刪除重復(fù)記錄的實(shí)現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266