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 p1 from Person p1, Person p2
where p1.Email=p2.Email and p1.Id > p2.Id;
本文介绍了一个SQL查询案例,用于从名为Person的表中删除所有重复的电子邮件条目,仅保留基于最小Id的唯一电子邮件。通过使用子查询和比较两个表中的Id和Email字段,可以有效地实现这一目标。
225

被折叠的 条评论
为什么被折叠?



