Postgresql批量按照顺序更新某一个字段

如批量更新采购订单行sequence字段,按照订单行id的顺序赋值1,2,3,4...:

UPDATE purchase_order_line_copy1
SET sequence = subquery.new_sequence
FROM (
    SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS new_sequence
    FROM purchase_order_line
		WHERE order_id = 748
) AS subquery
WHERE purchase_order_line_copy1.id = subquery.id 
and purchase_order_line_copy1.order_id = 748

限制订单为748的订单行,更新订单行sequence字段,效果如下:

Postgresql批量按照顺序更新某一个字段_第1张图片

你可能感兴趣的:(PostgreSQL,postgresql)