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 |
+----+------------------+
# Write your MySQL query statement below

delete from Person where Id not in
(
select Id from
(
select Id,
if(STRCMP(@prev, Email)=0, false, true) as Diff,
@prev := Email from
(select * from Person p2 order by Email, Id asc) x,
(select @prev := '') y
) t where t.Diff=true
)

  

LeetCode Database: Delete Duplicate Emails的更多相关文章

  1. LeetCode DB : Delete Duplicate Emails

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

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

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

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

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

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

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

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

  6. LeetCode DB: Duplicate Emails

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

  7. leetcode【sql】 Delete Duplicate Emails

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

  8. LeetCode - Delete Duplicate Emails

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

  9. LeetCode——Delete Duplicate Emails(巧用mysql临时表)

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

随机推荐

  1. Xlib: connection to ":0.0" refused by server Xlib: No protocol specified解决方案

    Xlib: connection to ":0.0" refused by server Xlib:  No protocol specified 解决办法: 1. 退出oracl ...

  2. angularjs transclude demo

    <!doctype html> <html lang="en" ng-app="expanderModule"> <head> ...

  3. sleep和wait到底什么区别

    wait是在当前线程持有wait对象锁的情况下,暂时放弃锁,并让出CPU资源,并积极等待其它线程调用同一对象的notify或者notifyAll方法.注意,即使只有一个线程在等待,并且有其它线程调用了 ...

  4. Linux 下sleep()函数

    调试程序发现起了一个子线程后,主线程上的sleep不生效了,看到这才明白... — Function: unsigned int sleep (unsigned int seconds) The sl ...

  5. ActionScript 设置元件色彩属性

        var clr:Color = new Color(mc);     var ct:Object = {ra:100, rb:0, ga:100, gb:0, ba:100, bb:0, aa ...

  6. android 更改avd路径

    第一种方法,适合还没有建立 AVD 的情况 即:在计算机右击的属性 选择环境变量,然后添加一个用户的环境变量,名字为 "ANDROID_SDK_HOME”,然后把变量值改为你想将" ...

  7. [HDOJ5584]LCM Walk(数论,规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 给一个坐标(ex, ey),问是由哪几个点走过来的.走的规则是x或者y加上他们的最小公倍数lcm ...

  8. 转:CSS布局:Float布局过程与老生常谈的三栏布局

    使用CSS布局网页,那是前端的基本功了,什么两栏布局,三栏布局,那也是前端面试的基本题了.一般来说,可以使用CSSposition属性进行布局,或者使用CSSfloat属性布局.前者适合布局首页,因为 ...

  9. asp.net web forms和asp.net mvc比较

    ASP.NET Webforms Behind Code的好处和存在的问题 ASP.NET Webforms是一个RAD/VISUAL(快速可视化)的Web程序开发技术.也就是说,开发者简单地拖拽控件 ...

  10. WEBUS2.0 In Action - [源代码] - C#代码搜索器

    最近由于工作的需要, 要分析大量C#代码, 在数万个cs文件中搜索特定关键词. 这是一项非常耗时的工作, 用Notepad++要运行接近半个小时. 于是我利用WEBUS2.0 SDK创建了一个代码搜索 ...