LeetCode: 196. Delete Duplicate Emails

LeetCode: 196. Delete Duplicate Emails

题目描述

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

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

Id is the primary key column for this table.
For example, after running your query, the above Person table should have the following rows:

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

Note:

Your output is the whole Person table after executing your sql. Use delete statement.

解题思路

-- 删除部分数据行的搜索型 DELETE
-- DELETE语句的删除对象并不是表或者列,而是记录(行)
DELETE FROM <表名> WHERE <条件>;

AC 代码

DELETE P2 FROM Person AS P1, Person AS P2 WHERE P1.Id < P2.Id AND P1.Email = P2.Email;
全部评论

相关推荐

孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
程序员猪皮:看不到八股什么意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务