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

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

    Same as above with corrections. Works but definitely not pretty.

       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
    

    Honestly, better to use the above answers.

提交回复
热议问题