更新 table 中的列,该列具有来自同一列的真实 ID 的临时 ID

Update a column in table which has a temp id with real id from the same column

我遇到了一个独特的情况,我有一个名为 id 的列,它可能有临时 id,直到最终 id 出现,如:

id temp id
1 null
2 1
6 null
7 6

我想要一个将 table 更新为 :

的查询
id temp id
2 null
2 1
7 null
7 6

基本上,一旦 id 具有与 id 关联的临时 ID,我们只需使用 real_id.

更新所有这些临时 ID

不知道这是否可以实现。我尝试在更新后的 table 集中使用 case 语句,但这对我不起作用,而且还有成千上万条这样的记录。

临时 ID 以后是多余的没有问题,因为该 ID 不能自我重复,因此它不会成为分析的关注点,因为我们将仅使用 ID 进行分析

您可以使用 update:

update t
    set id = (select t2.id from t t2 where t2.tempid = t.id)
    where t.tempid is null;