How to find duplicates in 2 columns not 1

后端 未结 6 854
别跟我提以往
别跟我提以往 2020-12-04 05:59

I have a MySQL database table with two columns that interest me. Individually they can each have duplicates, but they should never have a duplicate of BOTH of them having th

6条回答
  •  有刺的猬
    2020-12-04 06:50

    I found it helpful to add a unqiue index using an "ALTER IGNORE" which removes the duplicates and enforces unique records which sounds like you would like to do. So the syntax would be:

    ALTER IGNORE TABLE `table` ADD UNIQUE INDEX(`id`, `another_id`, `one_more_id`);
    

    This effectively adds the unique constraint meaning you will never have duplicate records and the IGNORE deletes the existing duplicates.

    You can read more about eh ALTER IGNORE here: http://mediakey.dk/~cc/mysql-remove-duplicate-entries/

    Update: I was informed by @Inquisitive that this may fail in versions of MySql> 5.5 :

    It fails On MySQL > 5.5 and on InnoDB table, and in Percona because of their InnoDB fast index creation feature [http://bugs.mysql.com/bug.php?id=40344]. In this case first run set session old_alter_table=1 and then the above command will work fine

    Update - ALTER IGNORE Removed In 5.7

    From the docs

    As of MySQL 5.6.17, the IGNORE clause is deprecated and its use generates a warning. IGNORE is removed in MySQL 5.7.

    One of the MySQL dev's give two alternatives:

    • Group by the unique fields and delete as seen above
    • Create a new table, add a unique index, use INSERT IGNORE, ex:
    CREATE TABLE duplicate_row_table LIKE regular_row_table;
    ALTER TABLE duplicate_row_table ADD UNIQUE INDEX (id, another_id);
    INSERT IGNORE INTO duplicate_row_table SELECT * FROM regular_row_table;
    DROP TABLE regular_row_table;
    RENAME TABLE duplicate_row_table TO regular_row_table;
    

    But depending on the size of your table, this may not be practical

提交回复
热议问题