PostgreSQL; 使用 CASE 语句根据条件更新不同的列

Raj*_*jan 2 postgresql-9.5

我有一张有不同类型余额的表,我需要根据可用性从不同的列中扣除余额。

这是我尝试使用的查询(示例),但没有用

update table_A
set case
    when column_A>table_B.balance then column_A
    when column_B>table_B.balance then column_B
    when column_C>table_B.balance then column_C
    end =value
from table_B
on table_A.Id=table_B.id
Run Code Online (Sandbox Code Playgroud)

谢谢!

Aki*_*ina 6

它是类似于(仅逻辑)

update table_A
set column_A = case when     (column_A>table_B.balance) then value else column_A end,
    column_B = case when not (column_A>table_B.balance) 
                     and     (column_B>table_B.balance) then value else column_B end,
    column_C = case when not (column_A>table_B.balance) 
                     and not (column_B>table_B.balance)
                     and     (column_C>table_B.balance) then value else column_C end
from table_B
on table_A.Id=table_B.id
Run Code Online (Sandbox Code Playgroud)