注:并非所有的数据库系统都支持 TOP 子句。
Persons 表:
Id |
LastName |
FirstName |
Address |
City |
1 |
Adams |
John |
Oxford Street |
London |
2 |
Bush |
George |
Fifth Avenue |
New York |
3 |
Carter |
Thomas |
Changan Street |
Beijing |
4 |
Obama |
Barack |
Pennsylvania Avenue |
Washington |
SQL Server 语法:
SELECT TOP number|percent 列 FROM 表
例:
如果我们希望从上面的表中选取头两条记录。语句如下:
SELECT TOP 2 * FROM Persons
结果:
Id |
LastName |
FirstName |
Address |
City |
1 |
Adams |
John |
Oxford Street |
London |
2 |
Bush |
George |
Fifth Avenue |
New York |
如果我们希望从上面的表中选取 50% 的记录。语句如下:
SELECT TOP 50 PERCENT * FROM Persons
结果:
Id |
LastName |
FirstName |
Address |
City |
1 |
Adams |
John |
Oxford Street |
London |
2 |
Bush |
George |
Fifth Avenue |
New York |
MySQL 和 Oracle 中 TOP 子句是等价的
MySQL 语法
SELECT 列FROM 表 LIMIT 数字
例
SELECT * FROM Persons LIMIT 5
Oracle 语法
SELECT 列FROM 表 WHERE ROWNUM <= 数字
例
SELECT * FROM Persons WHERE ROWNUM <= 5