zoukankan      html  css  js  c++  java
  • 196. Delete Duplicate Emails

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

    删除重复的地址,保留ID最小的

    MySQL(714ms):
    DELETE FROM Person
    WHERE Id NOT IN (
       SELECT * FROM(
          SELECT MIN(Id)
          FROM Person
          GROUP BY Email
       ) AS Mid
    );
  • 相关阅读:
    java面试
    java多态
    java多线程
    Java操作ElasticSearch
    liunx安装jdk
    linux安装Elasticsearch详细步骤
    java IO流
    java递归
    File文件类
    redis单机多节点集群
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/8321308.html
Copyright © 2011-2022 走看看