如何交换来自 Postgres 表中特定列的两个值?

Gan*_*thy 7 postgresql

考虑一个包含 id 和 name 属性的表测试。

那个值。

Id   |   Name
1    |   Raj
2    |   Kumar
Run Code Online (Sandbox Code Playgroud)

从上面的示例表中,我只知道 id,因此我需要通过 Id 交换名称,如下所示,

Id   |   Name
1    |   Kumar
2    |   Raj
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 10

update the_table
   set name = case id
                 when 1 then (select name from the_table where id = 2)
                 when 2 then (select name from the_table where id = 1)
              end
where id in (1,2);
Run Code Online (Sandbox Code Playgroud)

这假设它id是唯一的(例如主键)