+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id is the primary key column for this table. Each row of this table contains an email. The emails will not contain uppercase letters.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Input: Person table: +----+------------------+ | id | email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ Output: +----+------------------+ | id | email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | +----+------------------+ Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1.
筆記
先 SELECT 出不重複的 email ,這邊使用的是 GROUP BY email 再加上 MIN(id) ,同理,使用 MAX() 也可以。
遍歷 Person 這張表,如果這行資料並不在第一步 SELECT 出來的表裡,就把他刪掉。
1 2 3 4 5 6 7 8 9 10 11
DELETEFROM Person WHERE id NOTIN ( SELECT MIN(id) FROM Person GROUPBY email )