SQL 删除重复的电子邮箱

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

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Id 是这个表的主键。

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

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

Solution Code

delete
from Person
where Id not in
(
    select p.min
    from
    (
        select min(Id) as min
        from Person
        group by Email
    ) as p
)

-- other ways
delete p1 from person p1, person p2
where p1.email = p2.email and p1.id > p2.id;

原文地址:https://www.cnblogs.com/sumelt/p/10541389.html

时间: 2024-08-30 12:23:47

SQL 删除重复的电子邮箱的相关文章

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]

196. 删除重复的电子邮箱

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

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

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

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

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

解析:由题可知为删除数据语句.由条件重复的邮箱可得出查找条件之一是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

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

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