设为首页 加入收藏

TOP

leetcode数据库sql之DeleteDuplicateEmails
2015-11-21 01:57:09 来源: 作者: 【 】 浏览:0
Tags:leetcode 数据库 sql DeleteDuplicateEmails

leetcode原文引用:

?

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  |
+----+------------------+
我的sql语句:

?

DELETE FROM Person 
WHERE
    id NOT IN (SELECT 
        id
    FROM
        (SELECT 
            MIN(id) AS id
        FROM
            Person
        GROUP BY email) AS a);

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇javaweb中向postgreSQL插入当前时.. 下一篇sql查询一张表的重复数据

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: