1. 程式人生 > >【Leetcode】196. Delete Duplicate Emails (Easy)

【Leetcode】196. Delete Duplicate Emails (Easy)

1.題目

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

翻譯:寫一個SQL語句刪掉Person表中,所有email重複的實體(相同的保留一個),只保留I的最小的唯一email.

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

2.思路

①將Person表按Email分組,篩選每組裡面的最小Id值作為minId。

②刪除所有Id不在minId中的實體。

這個sql語句的思路很簡單,但是要注意的細節很多,遇到的錯誤我將在第4點中進行總結。

3.演算法

delete from Person
where Person.Id not in (
    select Person_derived.minId from(
        select min(Id) as minId from Person group by Email
    )Person_derived
)

4.總結

①遇到的錯誤1:

delete from Person
where Person.Id not in (
        select min(Id) as minId from Person group by Email
) 
You can't specify target table 'Person' for update in FROM clause

SQL特有的一個錯誤,不能篩選表中一部分元素,然後直接更新它,需要引入一箇中間表。

具體原因我猜測可能是和sql的設計有關係。



②遇到的錯誤2:

delete from Person
where Person.Id not in (
    select minId from(
        select min(Id) as minId from Person group by Email
    )
)
Your answer
Every derived table must have its own alias
 派生出的中間表必須要有個名字。

PS:Leetcode的Runtime很有意思,同一個演算法我提交兩次,一次打敗2%的使用者,一次打敗98%的使用者。

看來不能完全依賴這個指標,還是得學會計算時間複雜度呀。畢竟好幾次我都栽在時間複雜度上。