SQL duplicates with different primary keys

前端 未结 7 1348
青春惊慌失措
青春惊慌失措 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:44

    --Remove Duplicate Rows with different ID SQL SERVER
    
    CREATE TABLE #TempTable
    (
        Id        int,
        Firstname varchar(20),
        Lastname  varchar(20)
    )
    
    INSERT INTO #TempTable( Id, Firstname, Lastname) SELECT min(Id)as Id, Firstname, Lastname 
    FROM UserTable 
    GROUP BY  Firstname, Lastname
    
    delete from UserTable where Id not in(select Id from #TempTable)
    
    drop #TempTable
    

提交回复
热议问题