刚刚那道题是困难也就算了,为什么这道简单题也做了这么久。。。。
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。
这道题其实思路很清晰啊,如下,
#找出重复的email
#对于重复的email选出id最小
#删除除了刚刚选出id外其余id的记录
首先前两个要求很好写语句
SELECT MIN(Id) AS Id, Email
FROM Person
GROUP BY Email HAVING COUNT(Email) > 1
这就很简单找出来了重复的那一组,我们应该保留的那条数据
下面删除这组除了该条记录以外的数据即可
首先需要找出该组内除了这条数据以外的数据,我们可以用not in 也可以直接用比它大来表示
SELECT p2.Id
FROM (SELECT MIN(Id) AS Id, Email
FROM Person
GROUP BY Email HAVING COUNT(Email) > 1
) AS p1
LEFT JOIN Person AS p2
ON p1.Email = p2.Email
WHERE p1.Id < p2.Id
这个技巧应该一目了然了,连接两个一样的表,首先设定同组,其次id大小。
最后其实用in来delete就可以了,但是leetcode的系统说不可以把delete和select放在一起,所以我看题解,是又select一遍……不知道真实场景是怎么样 不过有思路就可以了,最后给出完整的答案
DELETE FROM Person
WHERE Id IN (SELECT a.Id
FROM(SELECT p2.Id
FROM (SELECT MIN(Id) AS Id, Email
FROM Person
GROUP BY Email HAVING COUNT(Email) > 1
) AS p1
LEFT JOIN Person AS p2
ON p1.Email = p2.Email
WHERE p1.Id < p2.Id) AS a
)