题目:
编写一个 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 |
+----+------------------+
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/delete-duplicate-emails
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一: DELETE+子查询
DELETE FROM Person
WHERE Id NOT IN (
SELECT id FROM
(
SELECT MIN(Id) AS Id
FROM Person
GROUP BY Email
) AS temp -- 此处需使用临时表,否则会发生报错
)
解法二: DELETE+内链接
DELETE p1 FROM Person p1,
Person p2
WHERE
p1.Email = p2.Email AND p1.Id > p2.Id
思路:
- 内链接,形成笛卡尔积
- 记录中如果 email相同,但是id不同说明有重复
- 并且,id大的email需要删除,从而保留小id的数据
1.获得笛卡尔积
select * from Person p1,Peson p2;
2. 获得p1.email = p2.emaild的结果
select * from person p1,person p2 where p1.email = p2.email
3.根据题目要求我们要保留id小的那条数据,将id大的重复数据删除
通过p1.id > p2.id找出id大的重复数据
select * from person p1,person p2 where p1.email = p2.email and p1.id > p2.id
4.再将这条数据删除即可
delete p1.* from Person p1,Person p2 where p1.email = p2.email and p1.id > p2.id