Find duplicate records in MySQL

后端 未结 23 2820
别跟我提以往
别跟我提以往 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.

    0 讨论(0)
  • 2020-11-21 23:41

    The key is to rewrite this query so that it can be used as a subquery.

    SELECT firstname, 
       lastname, 
       list.address 
    FROM list
       INNER JOIN (SELECT address
                   FROM   list
                   GROUP  BY address
                   HAVING COUNT(id) > 1) dup
               ON list.address = dup.address;
    
    0 讨论(0)
  • 2020-11-21 23:41
    SELECT date FROM logs group by date having count(*) >= 2
    
    0 讨论(0)
  • 2020-11-21 23:43

    Why not just INNER JOIN the table with itself?

    SELECT a.firstname, a.lastname, a.address
    FROM list a
    INNER JOIN list b ON a.address = b.address
    WHERE a.id <> b.id
    

    A DISTINCT is needed if the address could exist more than two times.

    0 讨论(0)
  • 2020-11-21 23:43

    we can found the duplicates depends on more then one fields also.For those cases you can use below format.

    SELECT COUNT(*), column1, column2 
    FROM tablename
    GROUP BY column1, column2
    HAVING COUNT(*)>1;
    
    0 讨论(0)
  • 2020-11-21 23:43
     SELECT firstname, lastname, address FROM list
     WHERE 
     Address in 
     (SELECT address FROM list
     GROUP BY address
     HAVING count(*) > 1)
    
    0 讨论(0)
提交回复
热议问题