zoukankan      html  css  js  c++  java
  • 删除重复的电子邮箱(SQL语句)

    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

    +----+------------------+
    | Id | Email |
    +----+------------------+
    | 1 | john@example.com |
    | 2 | bob@example.com |
    | 3 | john@example.com |
    +----+------------------+
    Id 是这个表的主键。
    例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

    +----+------------------+
    | Id | Email |
    +----+------------------+
    | 1 | john@example.com |
    | 2 | bob@example.com |
    +----+------------------+
     

    提示:

    执行 SQL 之后,输出是整个 Person 表。
    使用 delete 语句。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/delete-duplicate-emails
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    答案: 

    DELETE p1 from person p1, person p2 where p1.email=p2.email and p1.Id>p2.id;
    

      

    拓展:删除id小的 :

    DELETE p2 from person p1, person p2 where p1.email=p2.email and p1.Id>p2.id;
    

      

  • 相关阅读:
    Codeforces Round #394 (Div. 2) A. Dasha and Stairs
    HDU 1410 PK武林盟主
    HDU 3362 Fix(状压dp)
    P678-vect2.cpp
    Qt5获取本机网络信息
    Qt5标准文件对话框类
    nRF52832无法加载协议栈文件
    Debug记录(1)
    大小端模式
    nRF52832的SAADC
  • 原文地址:https://www.cnblogs.com/jiyanjiao-702521/p/12659550.html
Copyright © 2011-2022 走看看