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语句中,SELECTDELETE操作不能同时存在.

答案一

因此尝试新的解法,直接使用删除语句,结果如下所示:

delete p1 from Person p1, Person p2 where p1.id > p2.id and p1.Email = p2.Email;

此答案通过测试,但是效率较低.

答案二

后续思考中发现,可以使用临时表解决SELECTDELETE同时存在的问题,答案如下所示:

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临时表)的更多相关文章

  1. [LeetCode] Delete Duplicate Emails 删除重复邮箱

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  2. LeetCode - Delete Duplicate Emails

    Discription:Write a SQL query to delete all duplicate email entries in a table named Person, keeping ...

  3. Leetcode 182. Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  4. LeetCode Database: Delete Duplicate Emails

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  5. LeetCode DB : Delete Duplicate Emails

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  6. leetcode【sql】 Delete Duplicate Emails

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  7. leetcode 196. Delete Duplicate Emails 配合查询的delete

    https://leetcode.com/problems/delete-duplicate-emails/description/ 题意要对原来的数据表进行删除,不删除不行,它每次只输出原来那个表. ...

  8. LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)

    题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Jav ...

  9. 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 ...

随机推荐

  1. Java操作数据库——手动实现数据库连接池

    Java操作数据库——手动实现数据库连接池 摘要:本文主要学习了如何手动实现一个数据库连接池,以及在这基础上的一些改进. 部分内容来自以下博客: https://blog.csdn.net/soonf ...

  2. HTML空元素

    什么是空元素? 首先空元素下是没有子级节点和内容的.然后空元素是在开始标签中关闭的,也就是说空元素没有闭合标签的. 在HTML中的空元素有以下几个: <area> <base> ...

  3. SAP depreciation key config

    正常折旧 Configure 1.分配总帐科目 spro进入后台配置 –> Financial Accounting(New) –> Asset Accounting –> Depr ...

  4. 关于ImageView的几个常见问题

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/173 最近几天碰到了ImageView的backgroun ...

  5. URL Schemes of iOS

    About prefs:root=General&path=About Accessibility prefs:root=General&path=ACCESSIBILITY Airp ...

  6. Python—创建进程池的方式

    创建进程池 from multiprocessing import Pool import time,os result = [] # 存放所有worker函数的返回值 def worker(msg) ...

  7. 如何在文本编辑器中实现时间复杂度O(n/m)的搜索功能? BM算法

    //字符串匹配 public class StringCmp { //约定:A主串长 n ,B模式串 长m.要求:在A串中找到B串匹配的下标 //BM算法:从B串和A串尾部开始比较,希望一次将B串向后 ...

  8. 201871010123-吴丽丽《面向对象程序设计(Java)》第十三周学习总结

    201871010123-吴丽丽<面向对象程序设计(Java)>第十三周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  9. 数据嵌入js的关系图

    参照echarts官网,改了一下效果图: 数据放在了js里. 代码: <%@ page language="java" contentType="text/html ...

  10. SQL查询--内连接、外连接、自连接查询

    先创建2个表:学生表和教师表   1.内连接: 在每个表中找出符合条件的共有记录.[x inner join y on...] 第一种写法:只用where SELECT t.TEACHER_NAME, ...