Finding duplicate values in MySQL

前端 未结 25 2195
执笔经年
执笔经年 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:29

    Do a SELECT with a GROUP BY clause. Let's say name is the column you want to find duplicates in:

    SELECT name, COUNT(*) c FROM table GROUP BY name HAVING c > 1;
    

    This will return a result with the name value in the first column, and a count of how many times that value appears in the second.

    0 讨论(0)
  • 2020-11-22 04:29

    To find how many records are duplicates in name column in Employee, the query below is helpful;

    Select name from employee group by name having count(*)>1;
    
    0 讨论(0)
  • 2020-11-22 04:30
    SELECT varchar_col
    FROM table
    GROUP BY varchar_col
    HAVING COUNT(*) > 1;
    
    0 讨论(0)
  • 2020-11-22 04:32

    I improved from this:

    SELECT 
        col, 
        COUNT(col)
    FROM
        table_name
    GROUP BY col
    HAVING COUNT(col) > 1; 
    
    0 讨论(0)
  • 2020-11-22 04:33

    Try using this query:

    SELECT name, COUNT(*) value_count FROM company_master GROUP BY name HAVING value_count > 1;
    
    0 讨论(0)
  • 2020-11-22 04:35
    SELECT t.*,(select count(*) from city as tt
      where tt.name=t.name) as count
      FROM `city` as t
      where (
         select count(*) from city as tt
         where tt.name=t.name
      ) > 1 order by count desc
    

    Replace city with your Table. Replace name with your field name

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