在MySQL中,很容易根据订单结果更新表,例如:
SET @counter=1;
UPDATE task SET priority = (@counter:=@counter+1) ORDER BY createdAt ASC;
PostgreSQL上的等效查询是什么?
答案 0 :(得分:3)
您可以将UPDATE
与JOIN
一起使用到由ROW_NUMBER()
函数生成的行号表中(根据createdAt
的顺序选择行号):
UPDATE task
SET priority = p.priority
FROM (SELECT createdAt, ROW_NUMBER() OVER (ORDER BY createdAt) AS priority
FROM task) p
WHERE p.createdAt = task.createdAt