LeetCode——Delete Duplicate Emails(巧用mysql临时表)
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 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
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 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
Note:
Your output is the whole Person table after executing your sql. Use delete statement.
此题有两个解法:
我初步尝试用以下sql解决问题(要删除的记录Id肯定大于相同内容的Id):
delete p1 from Person p1, Person p2 where p2.id > p1.id and p2.Email = p1.Email;
但是无法通过,究其原因是在sql语句中,SELECT与DELETE操作不能同时存在.
答案一
因此尝试新的解法,直接使用删除语句,结果如下所示:
delete p1 from Person p1, Person p2 where p1.id > p2.id and p1.Email = p2.Email;
此答案通过测试,但是效率较低.
答案二
后续思考中发现,可以使用临时表解决SELECT与DELETE同时存在的问题,答案如下所示:
DELETE FROM Person
WHERE Id IN
(
SELECT Id FROM
(SELECT p1.Id as Id FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id) AS TEMP
)
此解决方案完美解决问题,且sql语句比较清晰明了.
PS:
如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!
LeetCode——Delete Duplicate Emails(巧用mysql临时表)的更多相关文章
- [LeetCode] Delete Duplicate Emails 删除重复邮箱
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- LeetCode - Delete Duplicate Emails
Discription:Write a SQL query to delete all duplicate email entries in a table named Person, keeping ...
- Leetcode 182. Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- LeetCode Database: Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- LeetCode DB : Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- leetcode【sql】 Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- leetcode 196. Delete Duplicate Emails 配合查询的delete
https://leetcode.com/problems/delete-duplicate-emails/description/ 题意要对原来的数据表进行删除,不删除不行,它每次只输出原来那个表. ...
- LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)
题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Jav ...
- leetcode 196. Delete Duplicate Emails
# 慢,内连接delete p1 from Person p1, Person p2 where p1.Email=p2.Email and p1.Id>p2.Id delete from Pe ...
随机推荐
- ZooKeeper 面试题
高强度训练第二十一天总结 1. ZooKeeper 面试题 ZooKeeper 是一个开放源码的分布式协调服务,它是集群的管理者,监视着集群 中各个节点的状态根据节点提交的反馈进行下一步合理操作.最终 ...
- Web APP自动更新
我们的手机软件每天都要经营,经常需要更新,比如程序的Bug,好的功能,好的洁面... ... 这就需要我们的用户打开web app时候自动更新客户端程序,而不是再去应用程序商店从新下载.今天的笔记就是 ...
- python中线程 进程 协程
多线程:#线程的并发是利用cpu上下文的切换(是并发,不是并行)#多线程执行的顺序是无序的#多线程共享全局变量#线程是继承在进程里的,没有进程就没有线程#GIL全局解释器锁#只要在进行耗时的IO操作的 ...
- django验证码captcha
官方文档 https://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation 使用命令安装pip instal ...
- 《icra16_slam_tutorial_tardos.pdf》
icra16_slam_tutorial_tardos.pdf EKF: https://www.cnblogs.com/gaoxiang12/p/5560360.html 7. 小结 卡尔曼滤波是递 ...
- JavaScript 代码执行顺序
一.先预处理后执行 在一个JavaScript文件或一个JavaScript代码块的内部,浏览器会先对代码进行预处理(编译),然后再执行. 预处理会跳过执行语句,只处理声明语句,同样也是按从上到下按顺 ...
- xml模块(了解)
目录 一.xml简介 二.Python使用xml 三.自己创建xml文档 一.xml简介 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在 ...
- 实现迭代器(__next__和__iter__)
目录 一.简单示例 二.StopIteration异常版 三.模拟range 四.斐波那契数列 一.简单示例 死循环 class Foo: def __init__(self, x): self.x ...
- Python程序中的进程操作-进程间数据共享(multiprocess.Manager)
目录 一.进程之间的数据共享 1.1 Manager模块介绍 1.2 Manager例子 一.进程之间的数据共享 展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大 ...
- Saiku ui-settings接口404错误避免(二十九)
Saiku ui-settings接口404错误避免 自己手动编译的saiku ,不知道为什么前端总是报错 /saiku/rest/saiku/info/ui-settings 404NotFo ...
