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

前端 未结 17 1997
夕颜
夕颜 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:23

    (untested) something like this may do the job

    WITH
    base AS
    (
        select *                   -- get the table
        from sometable
        order by name              -- in the desired order
    ),
    twenty AS
    (
        select *                   -- get the first 30 rows
        from base
        where rownum < 30
        order by name              -- in the desired order
    )
    select *                       -- then get rows 21 .. 30
    from twenty
    where rownum > 20
    order by name                  -- in the desired order
    

    There is also the analytic function rank, that you can use to order by.

提交回复
热议问题