Can there be a database-agnostic SQL query to fetch top N rows?

后端 未结 5 811
有刺的猬
有刺的猬 2021-01-28 05:33

We want to be able to select top N rows using a SQL Query. The target database could be Oracle or MySQL. Is there an elegant approach to this? (Needless to say, we\'re dealing w

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-28 06:26

    No. The syntax is different.

    You may, however, create views:

    /* Oracle */
    
    CREATE VIEW v_table
    AS
    SELECT  *
    FROM    (
            SELECT  *
            FROM    table
            ORDER BY
                    column
            )
    WHERE   rownum <= n
    
    /* MySQL */
    
    CREATE VIEW v_table
    AS
    SELECT  *
    FROM    table
    ORDER BY
            column
    LIMIT   n
    

提交回复
热议问题