Deleting duplicate rows from a table

邮差的信 提交于 2019-11-26 17:52:06

Yes, assuming you have a unique ID field, you can delete all records that are the same except for the ID, but don't have "the minimum ID" for their group of values.

Example query:

DELETE FROM Table
WHERE ID NOT IN
(
SELECT MIN(ID)
FROM Table
GROUP BY Field1, Field2, Field3, ...
)

Notes:

  • I freely chose "Table" and "ID" as representative names
  • The list of fields ("Field1, Field2, ...") should include all fields except for the ID
  • This may be a slow query depending on the number of fields and rows, however I expect it would be okay compared to alternatives

EDIT: In case you don't have a unique index, my recommendation is to simply add an auto-incremental unique index. Mainly because it's good design, but also because it will allow you to run the query above.

ALTER IGNORE TABLE 'table' ADD UNIQUE INDEX(your cols);

Duplicates get NULL, then you can delete them

Priyank
DELETE
FROM table_x a
WHERE rowid < ANY (
  SELECT rowid
  FROM table_x b
  WHERE a.someField = b.someField
   AND a.someOtherField = b.someOtherField
  )
WHERE (
  a.someField,
  a.someOtherField
  ) IN (
  SELECT c.someField,
   c.someOtherField
  FROM table_x c
  GROUP BY c.someField,
   c.someOtherField
  HAVING count(*) > 1
  )

In above query the combination of someField and someOtherField must identify the duplicates distinctively.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!