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
查询
- 从学生表中获取前两个数据集。
SELECT TOP 2 * FROM Student;
输出:
ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18
- 从“学生”表中获取总记录的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