Temptation

寻道之路 , 与您同行 !

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  321 随笔 :: 0 文章 :: 175 评论 :: 3 引用

对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,而且rownum不能以任何表的名称作为前缀。
 举例说明:
例如表:student(学生)表,表结构为:
ID       char(6)      --学号
name    VARCHAR2(10)   --姓名
create table student (ID char(6), name VARCHAR2(100));
insert into sale values('200001',‘张一’);
insert into sale values('200002',‘王二’);
insert into sale values('200003',‘李三’);
insert into sale values('200004',‘赵四’);
commit;

(1) rownum 对于等于某值的查询条件
如果希望找到学生表中第一条学生的信息,可以使用rownum=1作为条件。但是想找到学生表中第二条学生的信息,使用rownum=2结果查不到数据。因为rownum都是从1开始,但是1以上的自然数在rownum做等于判断是时认为都是false条件,所以无法查到rownum = n(n>1的自然数)。
SQL> select rownum,id,name from student where rownum=1;(可以用在限制返回记录条数的地方,保证不出错,如:隐式游标)
SQL> select rownum,id,name from student where rownum=1;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         1 200001 张一
SQL> select rownum,id,name from student where rownum =2;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------

(2)rownum对于大于某值的查询条件
   如果想找到从第二行记录以后的记录,当使用rownum>2是查不出记录的,原因是由于rownum是一个总是从1开始的伪列,Oracle 认为rownum> n(n>1的自然数)这种条件依旧不成立,所以查不到记录
SQL> select rownum,id,name from student where rownum >2;
ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
那如何才能找到第二行以后的记录呀。可以使用以下的子查询方法来解决。注意子查询中的rownum必须要有别名,否则还是不会查出记录来,这是因为rownum不是某个表的列,如果不起别名的话,无法知道rownum是子查询的列还是主查询的列。
SQL>select * from(select rownum no ,id,name from student) where no>2;
        NO ID     NAME
---------- ------ ---------------------------------------------------
         3 200003 李三
         4 200004 赵四
SQL> select * from(select rownum,id,name from student)where rownum>2;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------

(3)rownum对于小于某值的查询条件
如果想找到第三条记录以前的记录,当使用rownum<3是能得到两条记录的。显然rownum对于rownum<n((n>1的自然数)的条件认为是成立的,所以可以找到记录。
SQL> select rownum,id,name from student where rownum <3;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
        1 200001 张一
        2 200002 王二
综上几种情况,可能有时候需要查询rownum在某区间的数据,那怎么办呀从上可以看出rownum对小于某值的查询条件是人为true的,rownum对于大于某值的查询条件直接认为是false的,但是可以间接的让它转为认为是true的。那就必须使用子查询。例如要查询rownum在第二行到第三行之间的数据,包括第二行和第三行数据,那么我们只能写以下语句,先让它返回小于等于三的记录行,然后在主查询中判断新的rownum的别名列大于等于二的记录行。但是这样的操作会在大数据集中影响速度。
SQL> select * from (select rownum no,id,name from student where rownum<=3 ) where no >=2;
        NO ID     NAME
---------- ------ ---------------------------------------------------
         2 200002 王二
         3 200003 李三

(4)rownum和排序
Oracle中的rownum的是在取数据的时候产生的序号,所以想对指定排序的数据去指定的rowmun行数据就必须注意了。
SQL> select rownum ,id,name from student order by name;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         3 200003 李三
         2 200002 王二
         1 200001 张一
         4 200004 赵四
可以看出,rownum并不是按照name列来生成的序号。系统是按照记录插入时的顺序给记录排的号,rowid也是顺序分配的。为了解决这个问题,必须使用子查询
SQL> select rownum ,id,name from (select * from student order by name);
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         1 200003 李三
         2 200002 王二
         3 200001 张一
         4 200004 赵四
这样就成了按name排序,并且用rownum标出正确序号(有小到大)
笔者在工作中有一上百万条记录的表,在jsp页面中需对该表进行分页显示, 便考虑用rownum来作,下面是具体方法(每页
显示20条):
  “select * from tabname where rownum<20 order by name" 但却发现oracle却不能按自己的意愿来执行,而是先随便
取20条记录,然后再 order by,后经咨询oracle,说rownum确实就这样,想用的话,只能用子查询 来实现先排序,后
rownum,方法如下:
  "select * from (select * from tabname order by name) where  rownum<20",但这样一来,效率会较低很多。
  后经笔者试验,只需在order by 的字段上加主键或索引即可让oracle先按 该字段排序,然后再rownum;方法不变:
   “select * from tabname where rownum<20 order by name"

取得某列中第N大的行

select column_name from
(select table_name.*,dense_rank() over (order by column desc) rank from table_name)
where rank = &N;
 假如要返回前5条记录:

  select * from tablename where rownum<6;(或是rownum <= 5 或是rownum != 6)
假如要返回第5-9条记录:

select * from tablename
where …
and rownum<10
minus
select * from tablename
where …
and rownum<5
order by name
选出结果后用name排序显示结果。(先选再排序)

注意:只能用以上符号(<、<=、!=)。

select * from tablename where rownum != 10;返回的是前9条记录。
不能用:>,>=,=,Between...and。由于rownum是一个总是从1开始的伪列,Oracle 认为这种条件 不成立,查不到记录.

另外,这个方法更快:

select * from (
select rownum r,a from yourtable
where rownum <= 20
order by name )
where r > 10
这样取出第11-20条记录!(先选再排序再选)

要先排序再选则须用select嵌套:内层排序外层选。
rownum是随着结果集生成的,一旦生成,就不会变化了;同时,生成的结果是依次递加的,没有1就永远不会有2!
rownum 是在 查询集合产生的过程中产生的伪列,并且如果where条件中存在 rownum 条件的话,则:

1: 假如 判定条件是常量,则:
只能 rownum = 1, <= 大于1 的自然数, = 大于1 的数是没有结果的, 大于一个数也是没有结果的
即 当出现一个 rownum 不满足条件的时候则 查询结束   this is stop key!

2: 当判定值不是常量的时候
若条件是 = var , 则只有当 var 为1 的时候才满足条件,这个时候不存在 stop key ,必须进行 full scan ,对每个满足其他where条件的数据进行判定
选出一行后才能去选rownum=2的行…… 

posted on 2007-05-16 17:31 temptation 阅读(2445) 评论(16)  编辑 收藏 所属分类: 数据库--Oracle

评论

#1楼  2007-10-23 15:45 野猪&翔帅      
好文章,分析的很详细
刚好今天遇到了rownum上的使用问题
谢谢了,转载
  回复  引用  查看    

好,写的很清晰,一看完就能明白许多东西,受用了
  回复  引用    

#3楼  2007-12-04 15:32 limin [未注册用户]
select * from (
select rownum r,a from yourtable
where rownum <= 20
order by name )
where r > 10
這樣的語句在oracle中可以嗎?
其中order by可以用在子查詢中嗎?
我這里測試是不可以的。
  回复  引用    

#4楼  2007-12-04 15:34 limin [未注册用户]
--引用--------------------------------------------------
limin: select * from (
select rownum r,a from yourtable
where rownum &lt;= 20
order by name )
where r &gt; 10
這樣的語句在oracle中可以嗎?
其中order by可以用在子查詢中嗎?
我這里測試是不可以的。
可以的話,我的問題也就解決了。
--------------------------------------------------------

  回复  引用    

#5楼  2007-12-04 15:35 limin [未注册用户]
不過在這里還是學到了不少東西
  回复  引用    

#6楼 [楼主] 2007-12-05 08:57 temptation      
@limin

我测试过了,order by可以用在子查詢中的,你再查查你的sql语句吧
  回复  引用  查看    

#7楼  2007-12-05 16:09 limin [未注册用户]
select price
from (select price from orders where id='42011110130' and no_sht<'57110204' order by no_sht desc)
where rownum=1
以上代碼在oracle中運行,提示“遺漏右括弧”這樣的錯誤,去掉“order by no_sht desc”則沒有錯誤了,請樓主給點意見。
  回复  引用    

#8楼  2007-12-05 16:18 limin [未注册用户]
@temptation
select price
from (select price from orders where id='42011110130' and no_sht<'57110204' order by no_sht desc)
where rownum=1
以上代碼在oracle中運行,提示“遺漏右括弧”這樣的錯誤,去掉“order by no_sht desc”則沒有錯誤了,請樓主給點意見。
或者
select price
from (select price from orders where id='42011110130' and no_sht<'57110204' )
where rownum=“最后一行”
其中的rownum=“最后一行”又怎么實現?幫幫忙,謝謝!!!
  回复  引用    

#9楼 [楼主] 2007-12-05 16:53 temptation      
@limin

看你的意思 写了一个 你试下子

select price
from (select price from orders where id='42011110130' and no_sht<'57110204' )
where rowid=(select max(rowid) from orders)
  回复  引用  查看    

#10楼  2007-12-06 10:06 limin [未注册用户]
@temptation
將你的sql改成:
select price
from (select price from orders where id='42011110130' and no_sht<'57110204' )
where rowid=(select max(rowid) from (select price from orders where id='42011110130' and no_sht<'57110204' ))
就可以實現我要的功能,但運行起來速度很慢,還可以怎么優化一下呢?
不過比我原來的這個代碼好像還能快一點,我原來寫的是:
select a.price from orders a,(select max(no_sht) no_sht from orders
where id='42011110130'
and no_sht<'57110204')b
where a.no_sht=b.no_sht


  回复  引用    

#11楼 [楼主] 2007-12-06 10:56 temptation      
@limin

原来你的检索条件都一样的啊,那这句

select price
from orders
where
id='42011110130'
and no_sht<'57110204'
and rowid=(select max(rowid) from (select price from orders where id='42011110130' and no_sht<'57110204' ))
  回复  引用  查看    

#12楼  2007-12-06 10:59 limin [未注册用户]
@temptation
將你的sql最終改成:
select price,rownum,no_sht
from orders
where rowid=(select max(rowid)
from orders
where id='42011110130'
and no_sht<'57110204' )
速度依然很慢,當然也與我們的數據庫資料的多少有關,非常感謝你!

  回复  引用    

#13楼 [楼主] 2007-12-06 12:16 temptation      
@limin

不客气,自己能力有限,也优化不了什么

  回复  引用  查看    

#14楼  2008-03-14 00:53 匿名 [未注册用户]
好文章,谢谢指导

  回复  引用    

#15楼  2008-05-26 15:04 梁以薰 [未注册用户]
非常好,我刚好也遇到这个问题
  回复  引用    

good article, thanks.
  回复  引用    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索

相关链接:

历史上的今天:
2006-05-16 [转] Windows下Oracle安装图解
2006-05-16 [转] 网络协议汇总