美文网首页
leetcode:196. Delete Duplicate E

leetcode:196. Delete Duplicate E

作者: 唐僧取经 | 来源:发表于2018-09-18 08:32 被阅读0次

    196. Delete Duplicate Emails

    Description

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

    Your output is the whole Person table after executing your sql. Use delete statement.

    Answer

    delete p1 from Person as p1 ,Person as p2
    where p1.Email=p2.Email and p1.Id>p2.Id
    
    

    相关文章

      网友评论

          本文标题:leetcode:196. Delete Duplicate E

          本文链接:https://www.haomeiwen.com/subject/lizenftx.html