码迷,mamicode.com
首页 > 数据库 > 详细

Oracle取TOP N条记录

时间:2016-10-18 13:55:22      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

在SQL Server里面有top关键字可以很方便的取出前N条记录,但是Oracle里面却没有top的使用,类似实现取出前N条记录的简单方法如下:

方法1:利用ROW_NUMBER函数

取出前5条记录:

SELECT NO FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY NO) RNO, NO FROM ROWNUM_TEST
)WHERE RNO <= 5  ORDER BY NO ;

取出中间5条记录:

SELECT NO FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY NO) RNO, NO FROM ROWNUM_TEST
) WHERE RNO BETWEEN 5 AND 10 ORDER BY NO;

方法2:利用子查询

取出前5条记录:

SELECT NO FROM (
    SELECT NO FROM ROWNUM_TEST  ORDER BY NO
)WHERE ROWNUM <= 5  ORDER BY NO ;

取出中间5条记录:

SELECT NO  FROM (
    SELECT NO, ROWNUM RNO FROM (
        SELECT NO FROM ROWNUM_TEST  ORDER BY NO
    )
) WHERE RNO BETWEEN 5 AND 10  ORDER BY NO;

 

Oracle取TOP N条记录

标签:

原文地址:http://www.cnblogs.com/alsf/p/5972872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!