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. vue.js+THREE.js演示服务端3D模型流程总结

    three.js官网 ·场景搭建 使用npm或者其他获取安装three,就像npm i three,之后在需要演示模型的vue组件内import * as THREE from 'three',此时我 ...

  2. iPhone每一代的屏幕尺寸比例是多少?

    参考链接: https://www.jianshu.com/p/8f566ce3bc2c https://zhidao.baidu.com/question/1668756575750668747.h ...

  3. 【Gradle】Android Gradle 插件

    Android Gradle 插件 Android Gradle 插件简介 从Gradle角度来看,Android其实是Gradle的一个第三方插件,它是由Google的Android团队开发的.但从 ...

  4. 如何在Macbook上安装MySQL ?

    MySQL是常用的一款开源数据库,对各个平台都提供了支持,而Macbook又作为程序员的一款主力开发工具经常被使用.因此怎么在Macbook上安装MySQL进行程序开发也成了一项基本技能.下面来跟随本 ...

  5. Linux Shell之监测磁盘空间

    Linux Shell之监测磁盘空间 系统管理员的另一个重要任务就是监测系统磁盘的使用情况.不管运行的是简单Linux台式机还是大型的Linux服务器,我们都要知道还有多少空间可以留给应用程序.事实上 ...

  6. Mysql—添加用户并授权

    查询所有用户 -- 方式1 mysql> select host, user, password from mysql.user; -- 5.7版本之前的 mysql> select ho ...

  7. gcd套路变换

    gcd套路变换 GCD https://www.luogu.org/problem/P2568 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. $ 1& ...

  8. acwing 81. 扑克牌的顺子

    地址 https://www.acwing.com/problem/content/77/ 从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的. 2-10为数字本身,A为1,J为11, ...

  9. 14.web4

    右键查看源代码 先进行URL解码 解码之后可以得到一串 js 代码, 具体逻辑大概就是 var p1 = "67d709b2b"var p2 = "aa648cf6e87 ...

  10. Codeforces Round #594 (Div. 2) A. Integer Points 水题

    A. Integer Points DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS to ...