SQL TOP 子句用于从表中获取TOP 前N条或前X百分记录。
注意-所有数据库都不支持TOP子句,如,MySQL支持 LIMIT 子句来获取有限数量的记录,而Oracle使用 ROWNUM 命令来获取有限数量的记录。
Top - 语法
带有SELECT语句的TOP子句的基本语法如下。
SELECT TOP number|percent column_name(s) FROM table_name WHERE [condition]
Top - 示例
考虑具有以下记录的CUSTOMERS表-
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Learnfk | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
以下查询是SQL Server上的示例,它将从CUSTOMERS表中获取前3条记录。
SQL> SELECT TOP 3 * FROM CUSTOMERS;
这将产生以下输出-
+----+---------+-----+-----------+---------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---------+-----+-----------+---------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | +----+---------+-----+-----------+---------+
如果您使用的是MySQL服务器,那么这是一个等效示例-
SQL> SELECT * FROM CUSTOMERS LIMIT 3;
这将产生以下输出-
+----+---------+-----+-----------+---------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---------+-----+-----------+---------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | +----+---------+-----+-----------+---------+
如果使用的是Oracle服务器,则以下代码块具有等效的示例。
SQL> SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3;
这将产生以下输出-
+----+---------+-----+-----------+---------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---------+-----+-----------+---------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | +----+---------+-----+-----------+---------+
参考链接
https://www.learnfk.com/sql/sql-top-clause.html
标签:25,CUSTOMERS,+----+---------+-----+-----------+---------+,Top,2000.00,无涯,SQL,TOP From: https://blog.51cto.com/u_14033984/9258236