Find and remove duplicate rows by two columns

前端 未结 7 624
鱼传尺愫
鱼传尺愫 2021-02-01 08:52

I read all the relevant duplicated questions/answers and I found this to be the most relevant answer:

INSERT IGNORE INTO temp(MAILING_ID,REPORT_ID) 
SELECT DISTI         


        
7条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 09:44

    This works perfectly in any version of MySQL including 5.7+. It also handles the error You can't specify target table 'my_table' for update in FROM clause by using a double-nested subquery. It only deletes ONE duplicate row (the later one) so if you have 3 or more duplicates, you can run the query multiple times. It never deletes unique rows.

    DELETE FROM my_table
    WHERE id IN (
      SELECT calc_id FROM (
        SELECT MAX(id) AS calc_id
        FROM my_table
        GROUP BY identField1, identField2
        HAVING COUNT(id) > 1
      ) temp
    )
    

    I needed this query because I wanted to add a UNIQUE index on two columns but there were some duplicate rows that I needed to discard first.

提交回复
热议问题