我如何限制在postgres中更新的行数

How do i limit the number of rows updated in postgres

我正在尝试更新客户 table 中的记录,方法是将它们限制为 n 条记录,但是当我使用 offset 和 limit 关键字时出现错误。

我放在哪里

offset 0 limit 1

在更新语句子查询中作为子查询是这样的:

update customer set name = 'sample name' where customer_id in (142, 143, 144, 145 offset 0 limit 1);

当我尝试执行上面的更新语句时,出现错误:

ERROR: syntax error at or near "offset"

Note: limit does not have to be 1, it can be any number and same is true for offset

offsetlimit 处理行,而不是列表。

您可以将 in() 子句转换为使用一个子查询,该子查询 return 来自每个输入的一行

更新客户

set name = 'sample name' 
where customer_id in (select unnest(array[142, 143, 144, 145]) offset 0 limit 1);