Get list of duplicate rows in MySql

前端 未结 4 799
忘了有多久
忘了有多久 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:32
    select * from table AS t1 inner join
    (select max(id) As id,nachname,vorname, count(*) 
    from t1 group by nachname,vorname 
    having count(*) >1) AS t2 on t1.id=t2.id
    

    This should return ALL of the columns from the table where there is duplicate nachname and vorname. I recommend changing * to the exact columns that you need.

    Edit: I added a max(id) so that the group by wouldn't be a problem. My query isn't as elegant as I would want though. There's probably an easier way to do it.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-04 19:47

    The following query will give the list of duplicates :

    SELECT n1.* FROM table n1
    inner join table n2 on n2.vorname=n1.vorname and n2.nachname=n1.nachname
    where n1.id <> n2.id
    

    BTW The data you posted seems to be wrong "Doe" and "Knight" are a lastname, not a firstname :p.

    0 讨论(0)
  • 2021-02-04 19:47

    You can do it with a self-join:

    select distinct t1.id from t as t1 inner join t as t2 
    on t1.col1=t2.col1 and t1.col2=t2.col2 and t1.id<>t2.id
    

    the t1.id<>t2.id is necessary to avoid ids matching against themselves. (If you want only 1 row out of each set of duplicates, you can use t1.id<t2.id).

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