设为首页 加入收藏

TOP

[LeetCode]Delete Duplicate Emails,解题报告
2015-11-21 01:58:33 来源: 作者: 【 】 浏览:0
Tags:LeetCode Delete Duplicate Emails 解题 报告

题目

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

# 思路

题目的意思是:从Person表中找出Email重复的记录,只保留Id最小的记录,删除其他的重复记录。

因为是先查找,再删除,所以我们也分两步进行:

找出Email重复的记录
select p1.Id from Person as p1 inner join Person as p2 where p1.Email = p2.Email and p1.ID > p2.ID;
删除重复记录
delete from Person where Id in ($sql1);

思路虽然是好的,但是这样写却是错误的:

delete from Person where Id in (select p1.Id from Person as p1 inner join Person as p2 where p1.Email = p2.Email and p1.ID > p2.ID);

错误信息为:

You can't specify target table 'Person' for update in FROM clause

意思是:在MYSQL中,禁止在from子句中指定被更新的目标表。

所以,需要使用MYSQL规定的delete语法:

从数据表t1中把那些id值在数据表t2里有匹配的记录全删掉:
delete t1 from t1,t2 where t1.id = t2.id; or delete from t1 using t1,t2 where t1.id = t2.id;
从数据表t1里在数据表t2里没有匹配的记录查找出来并删除掉
delete t1 from t1 left join t2 on t1.id=t2.id where t2.id is null;

AC SQL

delete p1 from Person as p1 inner join Person as p2 on p1.ID > p2.ID and p1.Email = p2.Email;
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SQLServer的Lead和Lag实现 下一篇由ORM谈数据持久化

评论

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