Leetcode 196. Delete Duplicate Emails

来源:互联网 发布:手机故障检测软件 编辑:程序博客网 时间:2024/05/22 04:33

196. Delete Duplicate Emails

Total Accepted: 16136 Total Submissions: 85584 Difficulty: Easy

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

思路:

前面那个题是输出duplicate,现在是去除duplicate。用俩Person a, b 然后对比它们的EMAIL,如果一样删除ID大的哪个就可以了。

# Write your MySQL query statement belowDELETE FROM p1 USING Person p1 INNER JOIN Person p2WHERE p1.Email = p2.Email AND p1.Id > p2.Id;


0 0
原创粉丝点击