欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年08月20日 09:41:44   作者:不剪發(fā)的Tony老師  
這篇文章主要介紹了MySQL 如何查找并刪除重復記錄的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

創(chuàng)建示例表

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

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 字段存在重復數(shù)據;3、5 和 6 的 name 和 email 字段存在重復數(shù)據。

此時,如果我們想要為 email 創(chuàng)建一個唯一約束,將會返回錯誤:

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 字段中的重復記錄才能創(chuàng)建唯一約束。

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

如果想要找出 email 重復的數(shù)據,可以基于該字段進行分組統(tǒng)計,并且返回行數(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|

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

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|

另一種查找重復記錄的方法就是直接使用自連接查詢和 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,否則會某些數(shù)據(3、5、6)會返回多次。

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

如果我們想要找出 name 和 email 字段都重復的數(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|

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

刪除重復數(shù)據

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

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

假如我們想要刪除 email 重復的記錄,只保留其中一條,可以使用 DELETE FROM 語句實現(xiàn):

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

delete 語句通過連接找出需要刪除的記錄,以上示例保留了重復數(shù)據中的最大 id 對應的數(shù)據行。再次查詢 people 表:

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

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

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

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

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

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

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

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

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

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

這種方式需要注意的一個問題就是 create table … like 語句不會復制原表上的外鍵約束,需要手動添加。

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

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

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),同時按照 id 進行排序(order by id),然后為每個組內的數(shù)據分配一個編號;如果編號大于 1 就意味著存在重復的數(shù)據。

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

基于該查詢結果可以刪除重復的記錄:

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ù)據。

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

總結

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

相關文章

  • MySQL中如何將字符串替換簡單示例

    MySQL中如何將字符串替換簡單示例

    mysql是一種常用的關系型數(shù)據庫管理系統(tǒng),它提供了多種函數(shù)來處理和操作數(shù)據,下面這篇文章主要給大家介紹了關于MySQL中如何將字符串替換的相關資料,需要的朋友可以參考下
    2024-07-07
  • 如何選擇合適的MySQL日期時間類型來存儲你的時間

    如何選擇合適的MySQL日期時間類型來存儲你的時間

    這篇文章主要介紹了如何選擇合適的MySQL日期時間類型來存儲你的時間,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2020-08-08
  • linux下mysql5.7.17最新穩(wěn)定版本安裝教程

    linux下mysql5.7.17最新穩(wěn)定版本安裝教程

    這篇文章主要為大家詳細介紹了linux上mysql5.7.17最新穩(wěn)定版本安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • MySQL索引原理詳解

    MySQL索引原理詳解

    這篇文章主要介紹了MySQL索引原理詳解,索引是幫助MySQL高效獲取數(shù)據的排好序的數(shù)據結構,最重要的點是有序的,我們用索引就是為了快速的查找數(shù)據,如果一堆數(shù)據是無序的,程序只能挨個遍歷每個元素
    2022-08-08
  • mysql中的limit和offset用法詳解

    mysql中的limit和offset用法詳解

    這篇文章主要介紹了mysql中的limit和offset用法詳解,limit一般被用來排序,offset一般和limit組合使用,本文對這兩個函數(shù)進行詳細介紹,需要的朋友可以參考下
    2023-10-10
  • MySQL數(shù)據,查詢QPS,TPS數(shù)據方式

    MySQL數(shù)據,查詢QPS,TPS數(shù)據方式

    文章詳細介紹了查詢MySQL數(shù)據庫QPS和TPS的方法和工具,包括直接通過命令行、PerformanceSchema、mysqladmin、Prometheus、自動化腳本等,同時,還提供了優(yōu)化建議,如索引優(yōu)化、SQL調優(yōu)、事務控制和配置調優(yōu)
    2025-02-02
  • 計算機二級考試MySQL知識點 mysql alter命令

    計算機二級考試MySQL知識點 mysql alter命令

    這篇文章主要為大家詳細介紹了計算機二級考試MySQL知識點,詳細介紹了mysql中alter命令的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • php mysql連接數(shù)據庫實例

    php mysql連接數(shù)據庫實例

    這篇文章主要介紹了php mysql連接數(shù)據庫實例,需要的朋友可以參考下
    2016-09-09
  • MySQL內存表的特性與使用介紹

    MySQL內存表的特性與使用介紹

    臨時表和內存表都可以人工創(chuàng)建,但臨時表更多的作用是系統(tǒng)自己創(chuàng)建后,組織數(shù)據以提升性能,如子查詢,臨時表在多個連接之間不能共享。這里只討論內存表
    2013-02-02
  • Mysql ID生成策略的三種方法選擇及優(yōu)缺點

    Mysql ID生成策略的三種方法選擇及優(yōu)缺點

    mysql ID生成策略一般常用的有三種,包括自增、UUID 以及雪花算法,本文主要介紹了Mysql ID生成策略的三種方法選擇及優(yōu)缺點,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06

最新評論