删除mySQL 5.7.9中的所有重复行?

时间:2015-10-30 18:11:34

标签: mysql

我想删除mySQL表中使用GROUP BY重复的所有行。我的表格中有字段名称日期位置电子邮件,看起来像

+----------+---------------+----------+--------------------+
| M        | 1976-10-03    |        1 | m@gmail            |
| R        | 1982-03-26    |        2 | r@gmail.com        |
| C        | 1987-09-03    |        3 | c@gmail.com        |
| M        | 1976-10-03    |        1 | m@gmail            |
+----------+---------------+----------+--------------------+

我想要

+----------+---------------+----------+--------------------+           |
| R        | 1982-03-26    |        2 | r@gmail.com        |
| C        | 1987-09-03    |        3 | c@gmail.com        |
+----------+---------------+----------+--------------------+

我的尝试(来自类似问题的答案)

DELETE FROM ts1 WHERE * IN (SELECT * FROM ts1 GROUP BY * HAVING COUNT(*)>1);

错误在哪里?我知道我使用了太多*但是我想避免命名所有列,因为它们在我的实际表中太多了。请注意,我想检查整行的重复项。

1 个答案:

答案 0 :(得分:0)

您无法使用GROUP BY * - 您想使用GROUP BY name

DELETE FROM ts1 WHERE name IN (SELECT name FROM ts1 GROUP BY name HAVING COUNT(*)>1);

请注意,这会假设用户具有唯一的名称。

所以你可能真的想要检查他们的电子邮件:

DELETE FROM ts1 WHERE email IN (SELECT email FROM ts1 GROUP BY email HAVING COUNT(*)>1);