Find duplicate records in MySQL

后端 未结 23 2887
别跟我提以往
别跟我提以往 2020-11-21 23:12

I want to pull out duplicate records in a MySQL Database. This can be done with:

SELECT address, count(id) as cnt FROM list
GROUP BY address HAVING cnt >         


        
23条回答
  •  时光取名叫无心
    2020-11-21 23:38

    To quickly see the duplicate rows you can run a single simple query

    Here I am querying the table and listing all duplicate rows with same user_id, market_place and sku:

    select user_id, market_place,sku, count(id)as totals from sku_analytics group by user_id, market_place,sku having count(id)>1;
    

    To delete the duplicate row you have to decide which row you want to delete. Eg the one with lower id (usually older) or maybe some other date information. In my case I just want to delete the lower id since the newer id is latest information.

    First double check if the right records will be deleted. Here I am selecting the record among duplicates which will be deleted (by unique id).

    select a.user_id, a.market_place,a.sku from sku_analytics a inner join sku_analytics b where a.id< b.id and a.user_id= b.user_id and a.market_place= b.market_place and a.sku = b.sku;
    

    Then I run the delete query to delete the dupes:

    delete a from sku_analytics a inner join sku_analytics b where a.id< b.id and a.user_id= b.user_id and a.market_place= b.market_place and a.sku = b.sku;
    

    Backup, Double check, verify, verify backup then execute.

提交回复
热议问题