我想知道如何根据对某些字段进行排序(例如country
代码)将字段添加到表格(例如ISO
)?
我在SO周围搜索并找到following question,但解决方案基于MySQL。我尝试在Postgres 9.3中使用解决方案,它在:=
上给出了语法错误。我试过的是:
SELECT l.*,
@curRow := @curRow + 1 AS row_number
FROM country l ORDER BY ISO
JOIN (SELECT @curRow := 0) r;
如何将其改编为Postgres?
答案 0 :(得分:3)
您可以使用row_number()
window function:
SELECT country.*, ROW_NUMBER() OVER (ORDER BY iso)
FROM country
ORDER BY iso