How do I limit the number of rows returned by an Oracle query after ordering?

前端 未结 17 1551
夕颜
夕颜 2020-11-21 04:56

Is there a way to make an Oracle query behave like it contains a MySQL limit clause?

In MySQL, I can do this:

         


        
17条回答
  •  梦如初夏
    2020-11-21 05:17

    If you are not on Oracle 12C, you can use TOP N query like below.

    SELECT *
     FROM
       ( SELECT rownum rnum
              , a.*
           FROM sometable a 
       ORDER BY name
       )
    WHERE rnum BETWEEN 10 AND 20;
    

    You can even move this from clause in with clause as follows

    WITH b AS
    ( SELECT rownum rnum
          , a.* 
       FROM sometable a ORDER BY name
    ) 
    SELECT * FROM b 
    WHERE rnum BETWEEN 10 AND 20;
    

    Here actually we are creating a inline view and renaming rownum as rnum. You can use rnum in main query as filter criteria.

提交回复
热议问题