Remove duplicate rows in MySQL

前端 未结 25 3602
囚心锁ツ
囚心锁ツ 2020-11-21 04:33

I have a table with the following fields:

id (Unique)
url (Unique)
title
company
site_id

Now, I need to remove rows having same titl

25条回答
  •  孤独总比滥情好
    2020-11-21 05:07

    As of version 8.0 (2018), MySQL finally supports window functions.

    Window functions are both handy and efficient. Here is a solution that demonstrates how to use them to solve this assignment.

    In a subquery, we can use ROW_NUMBER() to assign a position to each record in the table within column1/column2 groups, ordered by id. If there is no duplicates, the record will get row number 1. If duplicate exists, they will be numbered by ascending id (starting at 1).

    Once records are properly numbered in the subquery, the outer query just deletes all records whose row number is not 1.

    Query :

    DELETE FROM tablename
    WHERE id IN (
        SELECT id
        FROM (
            SELECT 
                id, 
                ROW_NUMBER() OVER(PARTITION BY column1, column2 ORDER BY id) rn
            FROM output
        ) t
        WHERE rn > 1
    )
    

提交回复
热议问题