There are ways of doing this in optional parts of the standard, but a lot of databases support their own way of doing it.
(在标准的可选部分中有执行此操作的方法,但是许多数据库都支持其自己的执行方法。)
A really good site that talks about this and other things is http://troels.arvin.dk/db/rdbms/#select-limit .
(http://troels.arvin.dk/db/rdbms/#select-limit是一个非常好的网站,可以讨论此问题和其他问题。)
Basically, PostgreSQL and MySQL supports the non-standard:
(基本上,PostgreSQL和MySQL支持非标准的:)
SELECT...
LIMIT y OFFSET x
Oracle, DB2 and MSSQL supports the standard windowing functions:
(Oracle,DB2和MSSQL支持标准的窗口功能:)
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY key ASC) AS rownumber,
columns
FROM tablename
) AS foo
WHERE rownumber <= n
(which I just copied from the site linked above since I never use those DBs)
((由于我从未使用过这些数据库,所以我只是从上面链接的站点复制了该文件))
Update: As of PostgreSQL 8.4 the standard windowing functions are supported, so expect the second example to work for PostgreSQL as well.
(更新:从PostgreSQL 8.4开始,支持标准的窗口功能,因此希望第二个示例也适用于PostgreSQL。)
Update: SQLite added window functions support in version 3.25.0 on 2018-09-15 so both forms also work in SQLite.
(更新: SQLite在2018-09-15的3.25.0版本中添加了窗口功能支持,因此两种形式都可以在SQLite中使用。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…