Remove duplicates in large MySql table

前端 未结 6 2064
自闭症患者
自闭症患者 2021-01-06 14:44

I have a question about MySql. I have a table with 7.479.194 records. Some records are duplicated. I would like to do this:

insert into new_table 
  select *         


        
6条回答
  •  无人及你
    2021-01-06 15:37

    This will populate NEW_TABLE with unique values, and the id value is the first id of the bunch:

    INSERT INTO NEW_TABLE
      SELECT MIN(ot.id),
             ot.city,
             ot.post_code,
             ot.short_ccode
        FROM OLD_TABLE ot
    GROUP BY ot.city, ot.post_code, ot.short_ccode
    

    If you want the highest id value per bunch:

    INSERT INTO NEW_TABLE
      SELECT MAX(ot.id),
             ot.city,
             ot.post_code,
             ot.short_ccode
        FROM OLD_TABLE ot
    GROUP BY ot.city, ot.post_code, ot.short_ccode
    

提交回复
热议问题