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?
Add Unique Index on your table:
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:
Thanks to jveirasv for the answer above.
If you need to remove duplicates of a specific sets of column, you can use this (if you have a timestamp in the table that vary for example)
This doesn't use TEMP Tables, but real tables instead. If the problem is just about temp tables and not about table creation or dropping tables, this will work:
Add Unique Index on your table:
is work very well
An alternative way would be to create a new temporary table with same structure.
Then create the primary key in the table.
Finally copy all records from the original table while ignoring the duplicate records.
Now you can delete the original table and rename the new table.
If you are not using any primary key, then execute following queries at one single stroke. By replacing values:
It is always advisable to take backup of database before you play with it.