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

编写一个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

注意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

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

时间: 2024-10-02 20:00:35

leetcode196 删除重复的电子邮箱 Delete Duplicate Emails的相关文章

196. 删除重复的电子邮箱 | Delete Duplicate Emails

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +----+------------------+ | Id | Email | +----+------------------+ | 1 | [email protected] | | 2 | [email protected]

SQL 删除重复的电子邮箱

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ Id 是这个表的主键. 例如,在运行你的查询语句之后,

196. 删除重复的电子邮箱

题目描述 编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +----+------------------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +----+------------------+ Id是这个表的主键. 例如,在运行你

LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)

题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Java Solution: Runtime:  869 ms, faster than 33 % Memory Usage: N/A 完成日期:06/01/2019 关键点:p1.Id > p2.Id # Write your MySQL query statement below DELETE p1

LC-196 删除重复的电子邮箱

主要目标: 把表中的重复数据清除掉 思路: 先建一个表to_delete,用来保存查找出来的重复的数据记录 然后利用这个to_delete表去删除Person表中的重复记录 记得最后删除这个临时表to_delete,另外,创建to_delete时,不能使用temporary table. 参考链接: SQL查找删除重复行 原文地址:https://www.cnblogs.com/leo-lzj/p/9612749.html

数据库力扣题删除重复的电子邮箱

解析:由题可知为删除数据语句.由条件重复的邮箱可得出查找条件之一是Email属性的相等.由条件重复的邮箱里只保存最小的那个可得出查找条件之二是Id属性的比较. 答案:delete p1 from Person p1,Person p2 where p1.Id>p2.Id and p1.Email=p2.Email 原文地址:https://www.cnblogs.com/laixiaobin/p/11656324.html

Lc196-删除重复的电子邮箱

# Write your MySQL query statement below --题意:删除重复的邮箱,保留重复中邮箱id最小的 --逆向思维,删除每个邮箱中不是最小的 DELETE FROM person WHERE id NOT IN ( SELECT need.id FROM ( SELECT MIN(id) as id FROM person GROUP BY email ) as need ) 编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留

182. 查找重复的电子邮箱

Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +----+---------+ For example, your query should ret

leetcode查找重复的电子邮箱——oracle的group by 和having组合使用

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: +----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+ 根据以上输入,你的查询应返回以下结果: +---------+ | Email | +---------+ | a@b.com | +---------+ 说明:所有电子邮箱都是小写字母. 在S