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


第一次知道DELETE还可以这样用,想法是对的但是很傻的用了SELECT=.=

DELETE p1
FROM Person p1, Person p2
WHERE p1.Email = p2.Email AND
p1.Id > p2.Id

但是不明白的是 怎么就能知道ID在p1 p2中是有序的呢

EXPLANATION:

  • Take the table in the example

Id | Email

1 | john@example.com

2 | bob@example.com

3 | john@example.com

  • Join the table on itself by the Email and you'll get:

FROM Person p1, Person p2 WHERE p1.Email = p2.Email

p1.Id | p1.Email | p2.Id | p2.Email

1 | john@example.com | 1 | john@example.com

3 | john@example.com | 1 | john@example.com

2 | bob@example.com | 2 | bob@example.com

1 | john@example.com | 3 | john@example.com

3 | john@example.com | 3 | john@example.com

  • From this results filter the records that have p1.Id>p2.ID, in this case you'll get just one record:

AND p1.Id > p2.Id

p1.Id | p1.Email | p2.Id | p2.Email

3 | john@example.com | 1 | john@example.com

  • This is the record we need to delete, and by saying

DELETE p1

in this multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted, in this case just

p1.Id | p1.Email

3 | john@example.com

will be deleted


还有一个人的答案,在Sqlite可以运行但是MySQL不可以

DELETE FROM Person
    WHERE Id IN
    (SELECT P1.Id FROM Person AS P1, Person AS P2 
	     WHERE P1.Id > P2.Id AND P1.Email = P2.Email)
In mysql you must't update a table while using select clause , You can only do that step by step . However ,you can use a middle table as :

delete from Person where id not in(
select t.id from (
select min(id) as id from Person group by email
) t
)

MySQL Don't allow referring delete target table in sub query, a workaround is use ( select * from Person ) to get a new table.


delete from Person where Id in (
select p1.Id from (select * from Person) p1, (select * from Person) p2
where p1.Email = p2.Email and p1.Id > p2.Id )


Delete and Distinct are completely different, while delete alters the table, distinct only selects distinct values and doesn't alter table.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值