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]  |
| 3  | [email protected] |
+----+------------------+
Id is the primary key column for this table.

For example, after running your query, the above Person table should have the following rows:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
+----+------------------+


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

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
| 3  | [email protected] |
+----+------------------+
Id 是这个表的主键。

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
+----+------------------+

517ms
1 DELETE
2 FROM person
3 WHERE id NOT IN (SELECT t.id
4                  FROM (
5                         SELECT min(id) AS id
6                         FROM Person
7                         GROUP BY email
8                       ) t);


500ms

 1 # Write your MySQL query statement below
 2 DELETE
 3 FROM Person
 4 WHERE Id IN (
 5     SELECT Id
 6     FROM (
 7         SELECT
 8         Id, Email, (Email = @prev) AS dup, @prev := Email
 9         FROM (
10             SELECT Email, Id
11             FROM Person
12             ORDER BY 1, 2
13         ) AS sorted,
14         (SELECT @prev := NULL) AS init
15     ) AS t
16     WHERE dup IS TRUE
17 )


505ms

1 # Write your MySQL query statement below
2 delete from Person where Id not in(select t.Id from (select min(Id) id from Person b group by Email) t)

原文地址:https://www.cnblogs.com/strengthen/p/9744408.html

时间: 2024-10-09 02:13:13

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

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

196. 删除重复的电子邮箱

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

SQL 删除重复的电子邮箱

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ 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 196. 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 protect