当前位置:   article > 正文

LeetCode: 196. Delete Duplicate Emails

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 |
+----+------------------+
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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  |
+----+------------------+
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Note:

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

解题思路

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

AC 代码

DELETE P2 
FROM Person AS P1, Person AS P2 
WHERE P1.Id < P2.Id AND P1.Email = P2.Email;
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/799632
推荐阅读
相关标签
  

闽ICP备14008679号