在 Oracle 中,您可以执行以下操作:
select
rownum, -- here's the magic
name as country,
population as pop_mil
from
country
where
name like 'C%' and
member = true
order by
population;
并得到
rownum country pop_mil
------ -------- -------
1 Croatia 5
2 Chad 11
3 Cuba 11
4 Canada 35
5 Colombia 48
6 China 1360
请注意,rownum 与行在表中的存储方式无关。这是一个伪列,在完成所有过滤器和排序操作后枚举结果集中的行。
我知道row_number() over ...版本 >= 8.4(此处)中有一种解决方法(),但我的数据库是 8.3,并且近期没有迁移计划。
- 有没有办法在 PostgreSQL 8.3 中做到这一点?