leecode 数据库:196. 删除重复的电子邮箱 |
您所在的位置:网站首页 › 3409大写 › leecode 数据库:196. 删除重复的电子邮箱 |
导入数据: Create table If Not Exists Person (Id int, Email varchar(255)); Truncate table Person; insert into Person (id, email) values ('1', '[email protected]'); insert into Person (id, email) values ('2', '[email protected]'); insert into Person (id, email) values ('3', '[email protected]');表: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id是该表的主键列。 该表的每一行包含一封电子邮件。电子邮件将不包含大写字母。编写一个 SQL 删除语句来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件。 以 任意顺序 返回结果表。 (注意: 仅需要写删除语句,将自动对剩余结果进行查询) 查询结果格式如下所示。 示例 1: 输入: Person 表: +----+------------------+ | id | email | +----+------------------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +----+------------------+ 输出: +----+------------------+ | id | email | +----+------------------+ | 1 | [email protected] | | 2 | [email protected] | +----+------------------+ 解释: [email protected]重复两次。我们保留最小的Id = 1。解题思路: 题目要求 有两个条件 删除重复电子邮件 保留重复数据的那个id 最小的邮件 现在只有一张表,不过我们可以把一张表看作数据字段相同的两张表 这样我们就可以写我们的where 条件了 Person p1 , Person p2 p1.email = p2.email and p1.id > p2.id 这样我们就轻而易举的知道要删除的数据了 重复邮件 保留id最小 delete p1 from Person p1 ,Person p2 where p1.email = p2.email and p1.id > p2.id; # select 字段列表 from 表a 别名a join 表b 别名b on 条件..; 自连接语法 delete p1 from Person p1 join Person p2 on p1.email = p2.email and p1.id > p2.id;结语:小编能力有限 欢迎大家多多指教。 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/delete-duplicate-emails 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |