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

前端 未结 9 1230
北恋
北恋 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:13

    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)

    CREATE TABLE TableA_Verify AS SELECT * FROM TableA WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];
    
    DELETE FROM TableA;
    
    INSERT INTO TableA SELECT * FROM TAbleA_Verify;
    
    DROP TABLE TableA_Verify;
    
    0 讨论(0)
  • 2020-11-27 04:14

    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:

    SELECT DISTINCT * INTO TableA_Verify FROM TableA;
    
    DROP TABLE TableA;
    
    RENAME TABLE TableA_Verify TO TableA;
    
    0 讨论(0)
  • 2020-11-27 04:14

    Add Unique Index on your table:

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

    is work very well

    0 讨论(0)
  • 2020-11-27 04:14

    Tested in mysql 5.Dont know about other versions. If you want to keep the row with the lowest id value:

    DELETE n1 FROM 'yourTableName' n1, 'yourTableName' n2 WHERE n1.id > n2.id AND n1.member_id = n2.member_id and n1.answer_num =n2.answer_num
    

    If you want to keep the row with the highest id value:

    DELETE n1 FROM 'yourTableName' n1, 'yourTableName' n2 WHERE n1.id < n2.id AND n1.member_id = n2.member_id and n1.answer_num =n2.answer_num
    
    0 讨论(0)
  • 2020-11-27 04:23

    If you are not using any primary key, then execute following queries at one single stroke. By replacing values:

    # table_name - Your Table Name
    # column_name_of_duplicates - Name of column where duplicate entries are found
    
    create table table_name_temp like table_name;
    insert into table_name_temp select distinct(column_name_of_duplicates),value,type from table_name group by column_name_of_duplicates;
    delete from table_name;
    insert into table_name select * from table_name_temp;
    drop table table_name_temp
    
    1. create temporary table and store distinct(non duplicate) values
    2. make empty original table
    3. insert values to original table from temp table
    4. delete temp table

    It is always advisable to take backup of database before you play with it.

    0 讨论(0)
  • 2020-11-27 04:24

    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':

    alter table tableA add column duplicate tinyint(1) not null default '0';
    
    update tableA set
    duplicate=if(@member_id=member_id
                 and @quiz_num=quiz_num
                 and @question_num=question_num
                 and @answer_num=answer_num,1,0),
    member_id=(@member_id:=member_id),
    quiz_num=(@quiz_num:=quiz_num),
    question_num=(@question_num:=question_num),
    answer_num=(@answer_num:=answer_num)
    order by member_id, quiz_num, question_num, answer_num;
    
    delete from tableA where duplicate=1;
    
    alter table tableA drop column duplicate;
    

    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

    0 讨论(0)
提交回复
热议问题