Deleting Duplicate Rows in MySQL 5.0
The provided code employs a subquery to identify duplicate IDs in the posts table and attempt to delete them. However, this approach is incompatible with MySQL 5.0. To circumvent this issue, we need to modify the code to use a different syntax.
Rewriting the Code
The revised code to delete duplicate rows from the posts table is as follows:
DELETE FROM posts WHERE id IN ( SELECT id FROM ( SELECT id, COUNT(id) AS duplicate_count FROM posts GROUP BY id HAVING duplicate_count > 1 ) AS subquery )
This code uses a nested subquery to identify the duplicate IDs. The subquery first groups the rows by the id column and counts the number of occurrences of each ID. Rows with a duplicate count greater than 1 are then selected and their IDs are returned as a result set.
The outer query uses the IN operator to match the IDs returned by the subquery with the IDs in the posts table. This ensures that only duplicate rows are deleted.
Additional Notes
The above is the detailed content of How to Delete Duplicate Rows in MySQL 5.0?. For more information, please follow other related articles on the PHP Chinese website!