仅更新最大的行-基于顺序的更新



我想写一个查询来查找status=REQUEST_PENDING的LOWEST SEQUENCE,然后在特定条件后更新它。

例如:

序列<1>
person_name 状态
a 请求处理
b 请求_结束 2
c 请求结束 3

在MySQL中,您可以在更新查询中指定顺序和限制:

update t
set status = 'request_progressed'
where status = 'request_pending'
order by sequence
limit 1

在PostgreSQL中,您仍然需要order by和limit,但它们只能在子查询中使用:

update t
set status = 'request_progressed'
where (status, sequence) in (
select status, sequence
from t
where status = 'request_pending'
order by sequence
offset 0 rows fetch first 1 row only
)

最新更新