LeetCode:Delete Duplicate Emails - 刪除重複的郵箱數據

一、題目名稱sql

Delete Duplicate Emails(刪除重複的郵箱數據)數據庫

二、題目地址this

https://leetcode.com/problems/delete-duplicate-emails/命令行

三、題目內容code

寫一個SQL刪除表Person中全部的重複數據,對於重複的數據只保留Id最小的數據ci

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Id is the primary key column for this table.

例如,在執行完該SQL後,表中剩餘數據以下:leetcode

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

四、初始化數據庫腳本get

在MySQL數據庫中創建一個名爲LEETCODE的數據庫,用MySQL命令行中的source命令執行下面腳本:table

-- 執行腳本前必須創建名爲LEETCODE的DATABASE
USE LEETCODE;
  
DROP TABLE IF EXISTS Person;
CREATE TABLE Person (
  Id INT NOT NULL PRIMARY KEY,
  Email VARCHAR(50)
);

-- INSERT INTO Person (Id, Email) VALUES (1, 'john@example.com');
-- INSERT INTO Person (Id, Email) VALUES (2, 'bob@example.com');
-- INSERT INTO Person (Id, Email) VALUES (3, 'john@example.com');
INSERT INTO Person (Id, Email) VALUES (1, 'tsybius@example.com');
INSERT INTO Person (Id, Email) VALUES (2, 'tsybius@example.com');

五、解題SQL1class

下面是一個比較簡單的寫法

DELETE P2 FROM Person AS P1, Person AS P2
WHERE  P1.Email = P2.Email AND
       P1.Id < P2.Id;

六、解題SQL2

另外一種寫法是使用 NOT IN 來寫,但下面的寫法是有問題的,放在MySQL中執行時會報錯

DELETE FROM Person
WHERE  Id NOT IN (SELECT MIN(ID) MIN_ID
                  FROM   Person
                  GROUP  BY Email)

報錯信息以下:

ERROR 1093 (HY000): You can't specify target table 'Person' for update in FROM clause

這是由於SELECT語句不能直接出如今UPDATE語句(DELETE語句也是同樣)的WHERE子句中,把SQL改爲下面這樣就能夠AC了:

DELETE FROM Person
WHERE  Id NOT IN (SELECT TMP.MIN_ID
                  FROM   (SELECT MIN(ID) MIN_ID
                          FROM   Person
                          GROUP  BY Email) TMP);

END

相關文章
相關標籤/搜索