当前位置:网站首页>leetcode day5 删除重复的电子邮箱
leetcode day5 删除重复的电子邮箱
2022-07-28 17:47:00 【wyqgg123】
196. 删除重复的电子邮箱
难度简单
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。
±—±-----------------+
| Id | Email |
±—±-----------------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
±—±-----------------+
Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:
±—±-----------------+
| Id | Email |
±—±-----------------+
| 1 | [email protected] |
| 2 | [email protected] |
±—±-----------------+
提示:
执行 SQL 之后,输出是整个 Person 表。
使用 delete 语句。
题解:
通过题意我们不难看出,这题是需要使用delete语句删除重复的Email值,这里我们可以对该题目进行拆解,首先先将重复的Email查找出来,然后对查找道得Email结果删除即可。
1、查询出重复的Email结果
这里的实现就是通过自表连接就可以实现查询重复的Email,连接条件为a.Email等于b.Email,连接查询之后,我们需要在条件判断中将自己Email相等的行删除,这样获得的就是重复的Email.
select a.*
from Person a
left join Person b
on a.Email = b.Email
where a.Id > b.Id
运行结果如下表:
| id | |
|---|---|
| 3 | [email protected] |
2、将查询到的数据删除即可
这里只需要将select语句换成delete语句即可
sql代码
delete a.*
from Person a
left join Person b
on a.Email = b.Email
where a.Id > b.Id
删除重复数据之后表中的数据:
| Id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
边栏推荐
- Using Baidu easydl to realize chef hat recognition of bright kitchen and stove
- Smart contract security - overflow vulnerability
- Business visualization - let your flowchart "run" (4. Actual business scenario test)
- Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
- 中国首枚芯片邮票面世:内置120um超薄NFC芯片
- MySQL 8 creates master-slave replication based on Clone
- 华为入股南京芯视界,布局固态激光雷达芯片领域
- [notes] Apocalypse: list of practical experience and reflection of product managers
- Verification of chip power consumption performance: from dilemma to transcendence
- 英文翻译西班牙语-批量英文翻译西班牙工具免费
猜你喜欢

Pagoda panel construction novel CMS management system source code measurement - thinkphp6.0

ES6 new - arrow function

OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)

This customized keyboard turns me on~

SaltStack入门

English article translation - English article translation software - free batch translation

Adobe Flash player 34.0.0.92 and available version modification methods (2021-01-23

11、 学习MySQL UNION 操作符

WPF implements MessageBox message prompt box with mask

英文翻译意大利语-批量英文翻译意大利语工具免费
随机推荐
redis 主从架构(sizeof函数怎么计算)
MySQL8 基于clone创建主从复制
Redis master-slave architecture (how to calculate sizeof function)
The peak rate exceeds 2gbps! Qualcomm first passed 5g millimeter wave MIMO OTA test in China
Ardupilot software in the loop simulation and online debugging
English article translation - English article translation software - free batch translation
企业级分布式爬虫框架入门
Doxygen文档生成工具
架构基本概念和架构本质
Serial port receiving application ring buffer
This customized keyboard turns me on~
Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters
彻底理解位运算——与(&)、非(~)、或(|)、异或(^)
Investment of 3.545 billion yuan! Gree Group participates in Xiaomi industry fund
Rust Getting Started Guide (rustup, cargo)
华为入股南京芯视界,布局固态激光雷达芯片领域
MySQL8 Status Variables: Internal Temporary Tables and Files
Avoidance Adjusted Climbrate
[深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
Sudo rosdep init error: cannot download default