LeetCode: 196. Delete Duplicate Emails
LeetCode: 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 | +----+------------------+ Note:
Your output is the whole Person table after executing your sql. Use delete statement.
解题思路
-- 删除部分数据行的搜索型 DELETE
-- DELETE语句的删除对象并不是表或者列,而是记录(行)
DELETE FROM <表名> WHERE <条件>; AC 代码
DELETE P2 FROM Person AS P1, Person AS P2 WHERE P1.Id < P2.Id AND P1.Email = P2.Email;
汤臣倍健公司氛围 406人发布
查看9道真题和解析