码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode196 删除重复的电子邮箱 Delete Duplicate Emails

时间:2019-10-22 23:37:13      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:sel   set   innodb   直接   var   rop   person   leetcode   color   

编写一个SQL查询来删除Person表中所有重复的电子邮件,在重复的邮件中只保留Id最小的邮件。

技术图片

 

创建表和数据:

 

-- ----------------------------
-- Table structure for `person`
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
 `Id` int(11) DEFAULT NULL,
 `Email` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES (1,john@example.com);
INSERT INTO `person` VALUES (2,bob@example.com);
INSERT INTO `person` VALUES (3,john@example.com);

 

解法:

1.按email分组,找到每组id最小的行。

(
SELECT MIN(P.id) AS `Id`,P.Email
FROM Person AS P
GROUP BY P.email
) AS P2

从原表中DELETE掉不在表2中的行。

DELETE P1
FROM Person AS P1,
(
SELECT MIN(P.id) AS `Id`,P.Email
FROM Person AS P
GROUP BY P.email
) AS P2
WHERE P1.Id != P2.Id AND P1.Email = P2.Email

注意DELETEFROM之间,只放置了P1。说明只删除P1中的行,不删除P2中的行。

FROM后,P1和P2叉积。当然也可以应用内连接。

DELETE P1
FROM Person AS P1
JOIN 
(
SELECT MIN(P.id) AS `Id`,P.Email
FROM Person AS P
GROUP BY P.email
) AS P2
ON (P1.Id != P2.Id AND P1.Email = P2.Email)

2.既然DELETE 可以结合JOIN,直接表自连接,删除所有比当前行ID大的行。

DELETE P1
FROM Person AS P1 JOIN Person AS P2 ON (P1.email = P2.email AND P1.id > P2.id)

3.从集合的角度看。设,按email分组,找到每组id最小的行。 命名为集合A。那么,从全集U中保留集合A,删除U减去A的差集。再删除差集数据。应用LEFT JOIN。

 

 

 

DELETE U
FROM Person AS U
LEFT JOIN (
    SELECT MIN(id) AS `id`,email
    FROM Person
    GROUP BY email
) AS A ON (U.email = A.email AND U.id = A.id)
WHERE A.id IS NULL

 

leetcode196 删除重复的电子邮箱 Delete Duplicate Emails

标签:sel   set   innodb   直接   var   rop   person   leetcode   color   

原文地址:https://www.cnblogs.com/forever-fortunate/p/11723329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!