跨行

时间:2018-05-30 10:31:03

标签: sql postgresql sql-update

我想更新emp的姓氏和名字。意味着第一个emp名称应该更新为第五个emp。

id,firstname
1,john
2,pall
3,kevin
4,Alan

结果应该是

id,firstname
1,kevin
2,Alan
3,john
4,pall

1 个答案:

答案 0 :(得分:0)

根据您提供给我们的有限信息,解决方案看起来像这样:

update emp
   set firstname = case id
                    when 1 then (select firstname from emp where id = 3)
                    when 2 then (select firstname from emp where id = 4) 
                    when 3 then (select firstname from emp where id = 1)
                    when 4 then (select firstname from emp where id = 2)
                 end
where id in (1,2,3,4);