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  |
+----+------------------+

删除重复的地址,保留ID最小的

MySQL(714ms):
DELETE FROM Person
WHERE Id NOT IN (
   SELECT * FROM(
      SELECT MIN(Id)
      FROM Person
      GROUP BY Email
   ) AS Mid
);

 

相关文章:

  • 2022-03-06
  • 2021-12-12
  • 2021-06-16
  • 2022-12-23
  • 2021-08-30
  • 2022-12-23
  • 2021-08-17
  • 2021-09-30
猜你喜欢
  • 2021-07-14
  • 2021-08-03
  • 2021-06-08
  • 2021-10-11
  • 2022-02-14
  • 2021-10-01
  • 2022-02-21
相关资源
相似解决方案