在Oracle中,要按特定条件查询前N条记录,用个rownum就搞定了。 oracle
select * from emp where rownum <= 5 fetch
并且书上也告诫,不能对rownum用">",这也就意味着,若是你想用 大数据
select * from emp where rownum > 5 排序
则是失败的。要知道为何会失败,则须要了解rownum背后的机制: get
1 Oracle executes your query. it
2 Oracle fetches the first row and calls it row number 1. table
3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row. ast
4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth)。 dva
5 Go to step 3. 原理
了解了原理,就知道rownum>不会成功,由于在第三步的时候查询出的行已经被丢弃,第四步查出来的rownum仍然是1,这样永远也不会成功。
一样道理,rownum若是单独用=,也只有在rownum=1时才有用。
对于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对于rownum1的天然数)的条件认为是成立的,因此能够找到记录。
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标出正确序号(有小到大)