Write a SQL query to delete all duplicate email entries in a table named
Person
, keeping only unique emails based on itssmallest
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 | +----+------------------+
题意:删除email重复的记录,但是要保留id最小的一个
用时1285ms
delete p1 from Person as p1 inner join Person as p2 on p1.id > p2.id and p1.email = p2.email;