关键? oracle的rownum原理和?/strong>
Oracle的rownum原理和?
在Oracle中,要按特定条g查询前N条记录,用个rownum搞定了?
select * from emp where rownum <= 5
而且书上也告诫,不能对rownum?>"Q这也就意味着Q如果你想用
select * from emp where rownum > 5
则是p|的。要知道Z么会p|Q则需要了解rownum背后的机Ӟ
1 Oracle executes your query.
2 Oracle fetches the first row and calls it row number 1.
3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.
4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).
5 Go to step 3.
了解了原理,q道rownum>不会成功Q因为在W三步的时候查询出的行已经被丢弃,W四步查出来的rownum仍然?,q样永远也不会成功?
同样道理Qrownum如果单独?,也只有在rownum=1时才有用?
对于rownum来说它是oraclepȝ序分配Z查询q回的行的编Pq回的第一行分配的?Q第二行?Q依此类推,q个伪字D可以用于限制查询返回的总行敎ͼ而且rownum不能以Q何表的名UC为前~?
举例说明Q?
例如表:student(学生)表,表结构ؓQ?
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 对于{于某值的查询条g
如果希望扑ֈ学生表中W一条学生的信息Q可以用rownum=1作ؓ条g。但是想扑ֈ学生表中W二条学生的信息Q用rownum=2l果查不到数据。因为rownum都是?开始,但是1以上的自然数在rownum做等于判断是时认为都是false条gQ所以无法查到rownum = nQn>1的自然数Q?
SQL> select rownum,id,name from student where rownum=1;Q可以用在限制返回记录条数的地方Q保证不出错Q如Q隐式游标)
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
---------- ------ ---------------------------------------------------
Q?Qrownum对于大于某值的查询条g
如果xCW二行记录以后的记录Q当使用rownum>2是查不出记录的,原因是由于rownum是一个L?开始的伪列QOracle 认ؓrownum> n(n>1的自然数)q种条g依旧不成立,所以查不到记录
SQL> select rownum,id,name from student where rownum >2;
ROWNUM ID NAME
---------- ------ ---------------------------------------------------
那如何才能找到第二行以后的记录呀。可以用以下的子查询方法来解决。注意子查询中的rownum必须要有别名Q否则还是不会查录来Q这是因为rownum不是某个表的列,如果不v别名的话Q无法知道rownum是子查询的列q是L询的列?
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
---------- ------ ---------------------------------------------------
Q?Qrownum对于于某值的查询条g
如果x到第三条记录以前的记录,当用rownum<3是能得到两条记录的。显然rownum对于rownum<nQ?n>1的自然数Q的条g认ؓ是成立的Q所以可以找到记录?
SQL> select rownum,id,name from student where rownum <3;
ROWNUM ID NAME
---------- ------ ---------------------------------------------------
1 200001 张一
2 200002 王二
lg几种情况Q可能有时候需要查询rownum在某区间的数据,那怎么办呀从上可以看出rownum对小于某值的查询条g是h为true的,rownum对于大于某值的查询条g直接认ؓ是false的,但是可以间接的让它{为是true的。那必M用子查询。例如要查询rownum在第二行到第三行之间的数据,包括W二行和W三行数据,那么我们只能写以下语句,先让它返回小于等于三的记录行Q然后在L询中判断新的rownum的别名列大于{于二的记录行。但是这L操作会在大数据集中媄响速度?
SQL> select * from (select rownum no,id,name from student where rownum<=3 ) where no >=2;
NO ID NAME
---------- ------ ---------------------------------------------------
2 200002 王二
3 200003 李三
Q?Qrownum和排?
Oracle中的rownum的是在取数据的时候生的序号Q所以想Ҏ定排序的数据L定的rowmun行数据就必须注意了?
SQL> select rownum ,id,name from student order by name;
ROWNUM ID NAME
---------- ------ ---------------------------------------------------
3 200003 李三
2 200002 王二
1 200001 张一
4 200004 赵四
可以看出Qrownumq不是按照name列来生成的序受系l是按照记录插入时的序l记录排的号Qrowid也是序分配的。ؓ了解册个问题,必须使用子查?
SQL> select rownum ,id,name from (select * from student order by name);
ROWNUM ID NAME
---------- ------ ---------------------------------------------------
1 200003 李三
2 200002 王二
3 200001 张一
4 200004 赵四
q样成了按name排序Qƈ且用rownum标出正确序号Q有到大)

]]>