题目:
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 |
+—-+——————+
Answer:
思路:使用DELETE FROM,注意用MAX找出最大的id不行,因为有多组重复的情况。
# Write your MySQL query statement below
DELETE A1
FROM
person AS A1,
person AS A2
WHERE
A1.Email = A2.Email
AND A1.Id > A2.Id

本文介绍了一个SQL查询案例,目的是从Person表中删除所有重复的电子邮件条目,仅保留基于最小ID的唯一电子邮件。通过示例表格展示了运行查询前后的变化,并提供了具体的SQL删除语句。
3万+

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



