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 |
+----+------------------+
DELETE p1 FROM Person p1 INNER JOIN Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id;

这里p1和p2都是Person的别名。inner join等于join。所以p1 join p2应该是以下的结果:

------+------+-----------------+
|P1.Id| p2.Id|Email |
+-----+------+-----------------+
| 1 | 1 | john@example.com |
| 2 | 2 | bob@example.com |
| 3 | 3 | john@example.com |
+----+-------------------------+
然后p1.id>p2.id所以id为3的被删除了,但是因为p1和p2都是person事实上,只有一个id。为了理解,写成了两列。

补充一些关于sql语句的知识:
  • sql表别名的用法:as

  select * from kettleoutputtable a
  where a.os =2 and storename = 'anzhi'

  和

  select * from kettleoutputtable as a
  where a.os =2 and storename = 'anzhi'


  等效,也就是说别名的as可以省略!在表明后直接加上简单的名字就行了。

 
 

leetcode【sql】 Delete Duplicate Emails的更多相关文章

  1. 【SQL】182. Duplicate Emails

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

  2. [SQL]196. Delete Duplicate Emails

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

  3. 【SQL】ON DUPLICATE KEY UPDATE

    在实际应用中,经常碰到导入数据的功能,当导入的数据不存在时则进行添加,有修改时则进行更新, 在刚碰到的时候,第一反应是将其实现分为两块,分别是判断增加,判断更新,后来发现在mysql中有 ON DUP ...

  4. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  5. 【SQL】关于无法附加文件的错误

    [SQL]关于无法附加文件的错误 1.错误信息如下: 2.估计是权限问题右击属性,把权限开一下 3.然后就附加成功了~~ ——————————————————————————————————————— ...

  6. 【SQL】Oracle分页查询的三种方法

    [SQL]Oracle分页查询的三种方法 采用伪列 rownum 查询前10条记录 ? 1 2 3 4 5 6 7 8 9 10 11 [sql] select * from t_user t whe ...

  7. 【SQL】用Sql Server自动生产html格式的数据字典

    原文:[SQL]用Sql Server自动生产html格式的数据字典 本文软件环境:Sql Server 2008. 1.打开sql server管理器,给选定的表添加描述信息,给指定的字段添加描述信 ...

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

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

  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. Educational Codeforces Round 22.B 暴力

    B. The Golden Age time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. laravel5.4+vue+element-ui配置及简单使用

    前言:网上能找到的关于这个方面的教程实在是太少啦,所以踩了好多坑,特意来分享一下,原创哦.想要打包带走的小伙伴还请注明出处

  3. tp框架为什么验证码加载不出来?----- ob_clean() 可解决

    在用tp做验证码时,代码逻辑都正确,但就是加载不出图片来,如何解决呢?在创建验证码之前加上 ob_clean(); public function haha(){ ob_clean(); $v = n ...

  4. Oracle DECODE函数的用法详解

    Oracle DECODE函数使用方法: 1.比较大小select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值sign()函数根据某个值是0. ...

  5. 使用镜像仓库托管自己构建的Docker镜像

    自己构建的Docker镜像,默认存储在本机中,Docker有提供某些方式分享这些镜像,但不是主流的镜像分享方式,也有违于开源社区的共享精神. 本文介绍如何使用GitHub托管Dockerfile:使用 ...

  6. CJOJ 1494 【网络流24题】 搭配飞行员(二分图最大匹配)

    CJOJ 1494 [网络流24题] 搭配飞行员(二分图最大匹配) Description 飞行大队有若干个来自各地的驾驶员,专门驾驶一种型号的飞机,这种飞机每架有两个驾驶员,需一个正驾驶员和一个副驾 ...

  7. vue 自定义组件

    1.Vue.component('component-test', { props:{}, data:function(){ return{} }, mounted:function(){}, com ...

  8. SQL Server系列之SQL Server 2016 中文企业版详细安装步骤(超多图)

    1. 下载地址 下载地址 :https://www.microsoft.com/en-us/server-cloud/products/sql-server-2016/ 官方技术文档:https:// ...

  9. (转)Java compiler level does not match解决方法

    背景:工作中导入以前的项目,导出报Java compiler level does not match the versionof the installed Java project facet. ...

  10. (转)@ContextConfiguration注解说明

    场景:学习spring实战中相关的单元测试 1 正常使用 @ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件 1.1 单个文件 @ContextC ...