编写一个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‘,‘[email protected]‘); INSERT INTO `person` VALUES (‘2‘,‘[email protected]‘); INSERT INTO `person` VALUES (‘3‘,‘[email protected]‘);
解法:
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
注意:DELETE与FROM之间,只放置了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
原文地址:https://www.cnblogs.com/forever-fortunate/p/11723329.html
时间: 2024-10-02 20:00:35