在oracle中,按特定条件查询前N条记录,用个rownum就搞定了。
select * from emp where rownum<=5
而且书中也告诫,不能对rownum用">",这也就意味着,如果你想用
select * from emp where rownum>5则是失败的
rownum背后的机制:
1 oracle executes your query
2 oracle fethes 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
了解了原理,就知道rownum>不会成功,因为在第三部的时候查询出的行已经被丢弃,第四步查出来的rownum仍然是1,这样永远不会成功
同样的道理,rownum如果单独使用=,也只有rownum=1时才有用
查询"<".">"记录,使用子查询方法来解决,不过子查询中rownum必须要有别名,否则不会查出记录来
select * from (select rownum no,id from test ) where no >=2;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/16546776/viewspace-687849/ ,如需转载,请注明出处,否则将追究法律责任。