码疯窝

LeetCode 每日一题 — Delete Duplicate Emails

2015/04/07 03:57:03    分类: 日志连载    0人评论 次浏览

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  |
+----+------------------+

分析: 此题的确是很实用的, 以前经常会有用到这种问题需要处理.

DELETE t1 FROM Person t1
INNER JOIN Person t2
ON t1.Email = t2.Email
WHERE t1.Id > t2.Id
继续查看有关 日志连载的文章

0个访客评论