Get list of duplicate rows in MySql

前端 未结 4 802
忘了有多久
忘了有多久 2021-02-04 19:01

i have a table like this

ID     nachname     vorname
1       john         doe
2       john         doe
3       jim          doe
4       Michael     Knight
         


        
4条回答
  •  遇见更好的自我
    2021-02-04 19:39

    The general solution to your problem is a query of the form

    SELECT col1, col2, count(*)
    FROM t1
    GROUP BY col1, col2
    HAVING count(*) > 1
    

    This will return one row for each set of duplicate row in the table. The last column in this result is the number of duplicates for the particular values.


    If you really want the ID, try something like this:

    SELECT id FROM 
    t1, 
    ( SELECT col1, col2, count(*)
      FROM t1
      GROUP BY col1, col2
      HAVING count(*) > 1 ) as t2
    WHERE t1.col1 = t2.col1 AND t1.col2 = t2.col2 
    

    Haven't tested it though

提交回复
热议问题