一次更新多个值

akh*_*tty 3 postgresql update

我的任务是更新数据库中的几行。有两列,其中一列是ID,另一列是CURRENCY。我已经使用下面的更新语句更新了这些行:

update account set currency = 'INR' where id =15;
update account set currency = 'EURO' where id =12;
update account set currency = 'DOLLAR' where id =18;
update account set currency = 'Pound' where id =13; 
-- and so on.
Run Code Online (Sandbox Code Playgroud)

实际上,在这种情况下,由于行数相对较少,我可以使用这些更新语句轻松完成,但是如果有数千或数十万行怎么办?这是更新它们的唯一方法,还是有其他方法或脚本?

ype*_*eᵀᴹ 7

您可以使用表构造函数VALUES

update account 
set currency = nv.currency
from
    ( values
        (12, 'EURO'),
        (18, 'DOLLAR'),
        (13, 'Pound')
    ) as nv (id, currency)
where account.id = nv.id ;
Run Code Online (Sandbox Code Playgroud)