Find and remove duplicate rows by two columns

前端 未结 7 605
鱼传尺愫
鱼传尺愫 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:33

    NOTE: This solution is an alternative & old school solution.


    If you couldn't achieve what you wanted, then you can try my "oldschool" method:

    First, run this query to get the duplicate records:

    select   column1,
             column2,
             count(*)
    from     table
    group by column1,
             column2
    having   count(*) > 1
    order by count(*) desc
    

    After that, select those results and paste them into the notepad++:

    Now by using the find and replace specialty of the notepad++ replace them with; first "delete" then "insert" queries like this (from now on, for security reasons, my values will be AAAA).

    Special Note: Please make another new line for the end of the last line of your data inside notepad++ because regex matched the '\r\n' at the end of the each line:

    Find what regex: \D*(\d+)\D*(\d+)\D*\r\n

    Replace with string: delete from table where column1 = $1 and column2 = $2; insert into table set column1 = $1, column2 = $2;\r\n

    Now finally, paste those queries to your MySQL Workbench's query console and execute. You will see only one occurrences of each duplicate record.

    This answer is for a relation table constructed of just two columns without ID. I think you can apply it to your situation.

提交回复
热议问题