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 given by users to configurable questionnaires. The columns are member_id, quiz_num, question_num, answer_num
.
Somehow a few members got their answers submitted twice. So I need to remove the duplicated records, but make sure that one row is left behind.
There is no primary column so there could be two or three rows all with the exact same data.
Is there a query to remove all the duplicates?
Instead of
drop table TableA
, you could delete all registers (delete from TableA;
) and then populate original table with registers coming from TableA_Verify (insert into TAbleA select * from TAbleA_Verify
). In this way you won't lost all references to original table (indexes,... )As noted in the comments, the query in Saharsh Shah's answer must be run multiple times if items are duplicated more than once.
Here's a solution that doesn't delete any data, and keeps the data in the original table the entire time, allowing for duplicates to be deleted while keeping the table 'live':
This basically checks to see if the current row is the same as the last row, and if it is, marks it as duplicate (the order statement ensures that duplicates will show up next to each other). Then you delete the duplicate records. I remove the
duplicate
column at the end to bring it back to its original state.It looks like
alter table ignore
also might go away soon: http://dev.mysql.com/worklog/task/?id=7395