美文网首页
[Database]196. Delete Duplicate

[Database]196. Delete Duplicate

作者: Reflection_ | 来源:发表于2017-10-22 11:23 被阅读0次

题目: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 |
+----+------------------+

注意最后删除较大的ID

# Write your MySQL query statement below
DELETE a
FROM Person a, Person b
WHERE a.Email = b.Email AND a.ID>b.ID;  

相关文章

网友评论

      本文标题:[Database]196. Delete Duplicate

      本文链接:https://www.haomeiwen.com/subject/qqeguxtx.html