在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与行在表中的存储方式无关。它是一个伪列,在完成所有过滤和排序操作后枚举了结果集中的行。
我知道版本> = 8.4(here)有解决方法(row_number() over ...
),但我的数据库是8.3,并且不会很快迁移计划。
- 在PostgreSQL 8.3中有没有办法做到这一点?