Update one of 2 duplicates in an sql server database table

前端 未结 11 1223
长发绾君心
长发绾君心 2020-12-05 00:48

I have got a table that got a column with duplicate values. I would like to update one of a 2 duplicate values so for example row1 = tom and row2 = tom

11条回答
  •  情话喂你
    2020-12-05 01:23

    I have found this solution:

    My table devices already have data and the "serial" column should be unique. The "id" is a primary key. A random 6 digits value is concatenated after the original value.

    UPDATE devices 
    SET serial=CONCAT(serial,'_',LPAD(FLOOR(RAND() * 999999.99), 6, '0'))    
        where id in 
        (select * FROM( 
            SELECT d1.id as id 
            FROM devices as d1, devices as d2 
            WHERE d1.id <> d2.id and d1.serial=d2.serial) as subdevices
        )
    

提交回复
热议问题