当前位置:Gxlcms > 数据库问题 > SQL 删除重复的电子邮箱

SQL 删除重复的电子邮箱

时间:2021-07-01 10:21:17 帮助过:2人阅读

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Id 是这个表的主键。

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

Solution Code

delete 
from Person 
where Id not in
(
    select p.min
    from
    (
        select min(Id) as min 
        from Person 
        group by Email
    ) as p
)

-- other ways
delete p1 from person p1, person p2
where p1.email = p2.email and p1.id > p2.id;

SQL 删除重复的电子邮箱

标签:select   and   The   solution   strong   一个   --   查询   group   

人气教程排行