简体   繁体   中英

SQL query for top 5 results without the use of LIMIT/ROWNUM/TOP

如何在不使用LIMIT / ROWNUM / TOP的情况下从有序结果集中选择前5个条目,具体取决于DBMS?

Standard ANSI SQL solution:

SELECT *
FROM ( 
    SELECT col1, 
           col2,
           row_number() over (order by some_col) as rn
    FROM the_table
) t
WHERE rn <= 5

Works in Oracle, PostgreSQL, DB2, SQL Server, Sybase, Teradata and the upcoming Firebird 3.0 but not in MySQL as it still doesn't support windowing functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM