SELECT TOP 子句用于从数据库中获取有限数量的行。该子句在处理大型数据库时非常有用。
- 基本语法:
SELECT TOP value column1,column2 FROM table_name; value: number of rows to return from top column1 , column2: fields in the table table_name: name of table
- 使用百分比的语法
SELECT TOP value PERCENT column1,column2 FROM table_name; value: percentage of number of rows to return from top column1 , column2: fields in the table table_name: name of table
查询
- 从 Student 表中获取前两个数据集。
SELECT TOP 2 * FROM Student;
输出:
ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18
- 从 Student 表中获取 50% 的总记录。
SELECT TOP 50 PERCENT * FROM Student;
输出:
ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18 3 SUJIT ROHTAK XXXXXXXXXX 20
注意:要在 MySQL 和 Oracle 数据库上获得相同的功能,基本语法略有不同;
- 等效的语法如下:
- 对于 MySQL 数据库:
SELECT column1,column2 FROM table_name LIMIT value; column1 , column2: fields int the table table_name: name of table value: number of rows to return from top
- 对于 Oracle 数据库:
SELECT column1,column2 FROM table_name WHERE ROWNUM <= value; column1 , column2: fields int the table table_name: name of table value: number of rows to return from top