How to remove duplicate entries from a mysql db?

后端 未结 8 1346
南旧
南旧 2020-12-02 10:41

I have a table with some ids + titles. I want to make the title column unique, but it has over 600k records already, some of which are duplicates (sometimes several dozen ti

8条回答
  •  無奈伤痛
    2020-12-02 11:00

    The solution posted by Nitin seems to be the most elegant / logical one.

    However it has one issue:

    ERROR 1093 (HY000): You can't specify target table 'student' for update in FROM clause

    This can however be resolved by using (SELECT * FROM student) instead of student:

    DELETE FROM student WHERE id IN (
    SELECT distinct(s1.`student_id`) FROM (SELECT * FROM student) AS s1 INNER JOIN (SELECT * FROM student) AS s2
    WHERE s1.`sex` = s2.`sex` AND
    s1.`student_id` > s2.`student_id` AND
    s1.`sex` = 'M'
    ORDER BY `s1`.`student_id` ASC
    )
    

    Give your +1's to Nitin for coming up with the original solution.

提交回复
热议问题