zoukankan      html  css  js  c++  java
  • [LeetCode]196. 删除重复的电子邮箱(delete)

    题目

    编写一个 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  |
    +----+------------------+
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/delete-duplicate-emails
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    

    题解

    和select的笛卡尔积很像,具体删除表1的哪些会看where后面的东西。

    代码

    # Write your MySQL query statement below
    delete p1 from 
    Person p1,Person p2
    where p1.Email = p2.Email and p1.Id>p2.Id
    
  • 相关阅读:
    22. Generate Parentheses
    21. Merge Two Sorted Lists
    20. Valid Parentheses
    19. Remove Nth Node From End of List
    18. 4Sum
    JDK7新特性
    类Enum
    装饰设计模式
    模板设计模式
    反射
  • 原文地址:https://www.cnblogs.com/coding-gaga/p/12995785.html
Copyright © 2011-2022 走看看