SQL duplicates with different primary keys

前端 未结 7 1354
青春惊慌失措
青春惊慌失措 2021-01-24 19:56

I have a table in this form:

id | firstname | lastname
---+-----------+----------
1  | alex      | marti
2  | mark      | finger
3  | alex      | marti
4  | ted          


        
7条回答
  •  佛祖请我去吃肉
    2021-01-24 20:49

    You need to aggregate the id. If you need only the ID of one of them, for, say, deletion, you could do:

    select max(id) id, firstname, lastname from t group by firstname, lastname having count(*) > 1
    

    If you want both id's and know there will never be more than 2, you could do the following:

    select min(id) minid, max(id) maxid, firstname, lastname from t group by firstname, lastname having count(*) > 1
    

    If you want all duplicates, along with their id's, you'll have to use a derived table, as in Nitin Midha's answer.

提交回复
热议问题