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 |
+----+------------------+

考察删除中运用where条件

# Write your MySQL query statement below
delete from Person where id in (select * from (select A.id from Person as A, Person as B where A.id>B.id and A.Email = B.Email) X);

另外一种方法,也是用了嵌套查询

delete from Person where Id not in
(select min_id from (select min(Id) as min_id from Person group by Email) as Cid) ;

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

  1. LeetCode DB: Duplicate Emails

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

  2. LeetCode Database: Delete Duplicate Emails

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

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

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

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

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

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

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

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

  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. BZOJ 3357--[Usaco2004]等差数列(STL&DP)

    3357: [Usaco2004]等差数列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 516  Solved: 241[Submit][Statu ...

  2. 使用bash echo 输出回车转义

    输出回车 [root@~]# echo -e 'hello\n'hello 回车去掉 [root@~]# echo -n hello hello[root@~]#

  3. C# 字符串操作基本过程(Equals、Compare、EndsWith等处理方法)

    本文只介绍了比较方法,但是EndsWith,IndexOf等方法均采用相同的过程,先设置CultureInfo(一般情况下调用当前线程的CultureInfo,该语言文化可以通过控制面板设置),然后调 ...

  4. 五、CLR加载程序集代码时,JIT编译器对性能的产生的影响

    1.CLR首次加载代码造成的性能损失 四.CLR执行程序集中代码介绍了CLR在首次执行一个类的时,会初始化一个内部结构,然后当目标方法被首次调用时,JITComplier函数(JIT编译器)会验证IL ...

  5. tensorflow进阶篇-5(反向传播1)

    这里将讲解tensorflow是如何通过计算图来更新变量和最小化损失函数来反向传播误差的:这步将通过声明优化函数来实现.一旦声明好优化函数,tensorflow将通过它在所有的计算图中解决反向传播的项 ...

  6. hadoop2.7的目录结构

    1.$HADOOP_HOME/bin目录下文件及作用 文件名称 说明 hadoop 用于执行hadoop脚本命令,被hadoop-daemon.sh调用执行,也可以单独执行,一切命令的核心 2.$HA ...

  7. ADO.NET 4.5中的异步与流特性

    .NET 4.5为仍在选择直接与DataReader系列类打交道的.NET开发人员带来了一些新的异步与流特性支持.SqlDataReader允许开发人员在减少一些便利性的基础上获得更好的性能.例如,该 ...

  8. manjaro 添加当前用户到kvm

    原贴 https://askubuntu.com/questions/1050621/kvm-is-required-to-run-this-avd Check the ownership of /d ...

  9. 【胡思乱想】命令模式中,命令对象如何解耦Invoker和Receiver

    首先,我们得清楚为何要解耦? 耦合的坏处就是,牵一发而动全身,比如,当我更改了类A或其子类的时候,类B也要进行修改.这里,解除耦合,就意味着,即使你Receiver怎么改,添加了多少,删除了多少.我I ...

  10. PTA (Advanced Level) 1003 Emergency

    Emergency As an emergency rescue team leader of a city, you are given a special map of your country. ...