UPDATE table SET val1 = val1 + 1 where (val1 < $1 AND val1 >= ($1-$3));
update table set (val1,val2) = (val1 - $3, val2 + 1) WHERE val3 = $2 RETURNING %s
不幸的是,这给出了一个错误“无法在准备好的语句中插入多个命令”。有没有办法将这两个语句合并为一个?
是。在Postgres中你可以使用CTE:
with u1 as (
update table set val1 = val1 + 1 where (val1 < $1 AND val1 >= ($1-$3))
),
u2 as (
update table set (val1,val2) = (val1 - $3, val2 + 1) WHERE val3 = $2 RETURNING %s
)
select *
from u2;