Finding duplicate values in MySQL

前端 未结 25 2197
执笔经年
执笔经年 2020-11-22 04:04

I have a table with a varchar column, and I would like to find all the records that have duplicate values in this column. What is the best query I can use to find the duplic

相关标签:
25条回答
  • 2020-11-22 04:53

    I prefer to use windowed functions(MySQL 8.0+) to find duplicates because I could see entire row:

    WITH cte AS (
      SELECT *
        ,COUNT(*) OVER(PARTITION BY col_name) AS num_of_duplicates_group
        ,ROW_NUMBER() OVER(PARTITION BY col_name ORDER BY col_name2) AS pos_in_group
      FROM table
    )
    SELECT *
    FROM cte
    WHERE num_of_duplicates_group > 1;
    

    DB Fiddle Demo

    0 讨论(0)
提交回复
热议问题