今天碰见一个问题:
sql-1:
SELECT temp.*, ROWNUM num
FROM (select a.vcCityId,
a.vcCountryId,
a.vcCityName,
a.vcInitial,
a.vcCityDesc,
b.vcCountryName
from web2city a, web2country b
where a.vcCountryId = b.vcCountryId
order by to_number(a.vcCountryId),
a.vcInitial)temp
sql-2:
SELECT temp.*, ROWNUM num
FROM (select a.vcCityId,
a.vcCountryId,
a.vcCityName,
a.vcInitial,
a.vcCityDesc,
b.vcCountryName
from web2city a, web2country b
where a.vcCountryId = b.vcCountryId
order by to_number(a.vcCountryId),
a.vcInitial)temp
where ROWNUM <= 40
这两个sql的前40行结果不一样,为什么呢,其实是因为排序字段的结果不确定的缘故,由于这两个sql出来的结果有很多都是并列的,所以oracle给出的结果集列表不一定都是固定的。oracle会给出他以最快速度查到的满足条件的结果,但是却并不一定满足你的要求。修改需要加入一个唯一的字段进行排就不会出问题。
sql-3:(正解)
SELECT temp.*, ROWNUM num
FROM (select a.vcCityId,
a.vcCountryId,
a.vcCityName,
a.vcInitial,
a.vcCityDesc,
b.vcCountryName
from web2city a, web2country b
where a.vcCountryId = b.vcCountryId
order by to_number(a.vcCountryId),
a.vcInitial,
to_number(a.vccityid)) temp
where ROWNUM <= 40