编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+------------------+
Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
+----+------------------+
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/delete-duplicate-emails
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
DELETE t1
FROM
person t1
INNER JOIN person t2
WHERE
t1.id > t2.id
AND t1.email = t2.email;
数据脚本
create table `person` (
`id` int (11),
`email` varchar (120)
);
insert into `person` (`id`, `email`) values('1','[email protected]');
insert into `person` (`id`, `email`) values('2','[email protected]');
insert into `person` (`id`, `email`) values('3','[email protected]');