我有一个表“dummy”,其中包含以下列:
| id | name | position | updated_at | status |
我想用序列中的值更新列“position”,仅用于status = active和ordered desc by“updated_at”的记录。这可能吗?
由于'order by'子句,关闭查询但无效,是:
Update dummy d1 set position = nextval('positions_sequence')
from dummy d2 where d1.id = d2.id and d2.status = 'active'
order by d2.updated_at desc
答案 0 :(得分:1)
您可以使用循环使用存储过程轻松实现此目的:
FOR d1 IN SELECT * FROM dummy WHERE status = active order by updated_at desc LOOP
new_position := nextval('positions_sequence');
EXECUTE Update dummy set position = new_position where id = d1.id;
END LOOP;