使用pgsql中的不同值更新所有重复行

使用pgsql中的不同值更新所有重复行,sql,postgresql,Sql,Postgresql,我需要更新同一个表中具有不同值的重复行。 我的桌子是空的 table(id, phoneId(int), deviceId(int), userId(int)) 有些记录具有相同的deviceId或phoneId。比如说 id phoneId deviceId userId 1 23 3434 1235 2 23 5453 235 <---- same phoneId with 1 record 3

我需要更新同一个表中具有不同值的重复行。 我的桌子是空的

table(id, phoneId(int), deviceId(int), userId(int))
有些记录具有相同的
deviceId
phoneId
。比如说

id   phoneId   deviceId    userId
1    23        3434        1235
2    23        5453        235   <---- same phoneId with 1 record 
3    43        5453        2343  <---- same deviceId with 2 record
4    23        3434        6347  <---- same deviceId and phoneID with 1 record

只需更新重复的PhoneID,然后更新重复的DeviceID(假设表名为“t”)


问题是你可以得到锁链。P1-->D1-->P2-->D2。你想对这些做些什么?对不起,你能说得更具体一点吗
id   phoneId   deviceId    userId
1    23        3434        1235
2    235       5453        235   <---- phoneId changed to userId
3    43        2343        2343  <---- phoneId changed to userId
4    6347      6347        6347  <---- phoneId and deviceId changed to userId
UPDATE t SET phoneid=userid FROM (SELECT count(*),phoneid FROM t GROUP BY phoneid HAVING count(*)>1) AS foo WHERE t.phoneid=foo.phoneid;
UPDATE t SET deviceid=userid FROM (SELECT count(*),deviceid FROM t GROUP BY deviceid HAVING count(*)>1) AS foo WHERE t.deviceid=foo.deviceid;