How do I delete all the duplicate records in a MySQL table without temp tables

前端 未结 9 1355
北恋
北恋 2020-11-27 03:46

I\'ve seen a number of variations on this but nothing quite matches what I\'m trying to accomplish.

I have a table, TableA, which contain the answers gi

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 04:28

    Add Unique Index on your table:

    ALTER IGNORE TABLE `TableA`   
    ADD UNIQUE INDEX (`member_id`, `quiz_num`, `question_num`, `answer_num`);
    

    Another way to do this would be:

    Add primary key in your table then you can easily remove duplicates from your table using the following query:

    DELETE FROM member  
    WHERE id IN (SELECT * 
                 FROM (SELECT id FROM member 
                       GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
                      ) AS A
                );
    

提交回复
热议问题