Delete duplicated records from a table without pk or id or unique columns in mysql

前端 未结 4 1546
北荒
北荒 2021-01-20 22:30

I need to delete all the duplicated records from one of my tables the problem is that there isn\'t any id or unique or key column so I can\'t make something like this:

4条回答
  •  广开言路
    2021-01-20 23:19

    Adding a unique index (with all the columns of the table) with ALTER IGNORE will get rid of the duplicates:

    ALTER IGNORE TABLE table_name
      ADD UNIQUE INDEX all_columns_uq
        (phone, address, name, cellphone) ;
    

    Tested in SQL-Fiddle.

    Note: In version 5.5 (due to a bug in the implementation of fast index creation), the above will work only if you provide this setting before the ALTER:

    SET SESSION old_alter_table=1 ;
    

提交回复
热议问题