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

前端 未结 17 1557
夕颜
夕颜 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条回答
  •  梦毁少年i
    2020-11-21 05:02

    SQL Standard

    As I explained in this article, since version 12c Oracle supports the SQL:2008 Standard, which provides the following syntax to limit the SQL result set:

    SELECT
        title
    FROM
        post
    ORDER BY
        id DESC
    FETCH FIRST 50 ROWS ONLY
    

    Oracle 11g and older versions

    Prior to version 12c, to fetch the Top-N records, you had to use a derived table and the ROWNUM pseudocolumn:

    SELECT *
    FROM (
        SELECT
            title
        FROM
            post
        ORDER BY
            id DESC
    )
    WHERE ROWNUM <= 50
    

提交回复
热议问题