Find duplicate records in MySQL

后端 未结 23 2827
别跟我提以往
别跟我提以往 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-22 00:04

    This will select duplicates in one table pass, no subqueries.

    SELECT  *
    FROM    (
            SELECT  ao.*, (@r := @r + 1) AS rn
            FROM    (
                    SELECT  @_address := 'N'
                    ) vars,
                    (
                    SELECT  *
                    FROM
                            list a
                    ORDER BY
                            address, id
                    ) ao
            WHERE   CASE WHEN @_address <> address THEN @r := 0 ELSE 0 END IS NOT NULL
                    AND (@_address := address ) IS NOT NULL
            ) aoo
    WHERE   rn > 1
    

    This query actially emulates ROW_NUMBER() present in Oracle and SQL Server

    See the article in my blog for details:

    • Analytic functions: SUM, AVG, ROW_NUMBER - emulating in MySQL.

提交回复
热议问题