1. 程式人生 > >[LeetCode] Delete Duplicate Emails 刪除重複郵箱

[LeetCode] 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  | [email protected] |
| 2  | [email protected]  |
| 3  | [email protected]
| +----+------------------+ 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  | [email protected] |
| 2  | [email protected]
| +----+------------------+

這道題讓我們刪除重複郵箱,那我們可以首先找出所有不重複的郵箱,然後取個反就是重複的郵箱,都刪掉即可,那麼我們如何找出所有不重複的郵箱呢,我們可以按照郵箱群組起來,然後用Min關鍵字挑出較小的,然後取補集刪除即可:

解法一:

DELETE FROM Person WHERE Id NOT IN
(SELECT Id FROM (SELECT MIN(Id) Id FROM Person GROUP BY Email) p);

我們也可以使用內交讓兩個表以郵箱關聯起來,然後把相同郵箱且Id大的刪除掉,參見程式碼如下:

解法二:

DELETE p2 FROM Person p1 JOIN Person p2 
ON p2.Email = p1.Email WHERE p2.Id > p1.Id;

我們也可以不用Join,而直接用where將兩表關聯起來也行:

解法三:

DELETE p2 FROM Person p1, Person p2
WHERE p1.Email = p2.Email AND p2.Id > p1.Id;

類似題目:

參考資料: