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. mac 下tomcat启动报错 unknown host

    解决方法:sudo scutil --set HostName localhost

  2. mysql 转义字符和php addslashes

    遇到一个很奇怪的问题,json数据中含有中文: "mail":{"title":"\u6218\u529b\u8fbe\u4eba\u6d3b\u52 ...

  3. 浅谈ES5的const以及strict mode

    了解你使用的JavaScript版本是很重要的,因为不同版本的JavaScript对某些语法或者特性的支持情况是不一样的,下面就来举一些例子来说明一下.首先来看一下const关键字,学过比如Java, ...

  4. sizeof()与strlen()的区别

    首先需要说明的是sizeof和strlen都可以求长度,但是却有很大的区别,简单来说可以概括为以下几点: 1.sizeof是一个关键字,而strlen确实一个函数. 2.sizeof求的是字节长度,而 ...

  5. PHP empty函数判断0返回真还是假?

    最近项目中,遇到一个字段是 “是否启用”值为0,1 在查询时没想就写了 if ( isset($args_array['useFlg']) && !empty($args_array[ ...

  6. leetcode:Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1即反转二叉树,代码如下: /** * Defin ...

  7. request.getRequestDispatcher()和response.sendRedirect()

    request.getRequestDispatcher("/homeMainAction_mainUI.do").forward(getRequest(), getRespons ...

  8. 使用mysql索引的规则

    注意事项: 1)索引并不是越多越好 创建索引是会占用非常多的硬盘空间的,一般来说,一张表的索引的大小是其数据大小的2到3倍: 所以不要随便创建无用的索引,一般来说,只要给学用来做条件(where.or ...

  9. linux CPU loading calculate

    http://hi.baidu.com/lionpanther/item/e908c37abdaf1c3f71442380 #include <stdio.h>#include <s ...

  10. vijos 1379 字符串的展开

    23333333333333333 #include<iostream> #include<cstdio> #include<cstring> #include&l ...