java技术研究

统计

留言簿(3)

阅读排行榜

评论排行榜

关于翻页的sql结果集不确定问题

今天碰见一个问题:
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

posted on 2007-05-31 11:27 小秦 阅读(342) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: