W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
SQL TOP 子句用于從一張數(shù)據(jù)表中取回前 N 個或者 X% 的記錄。
注意:所有的數(shù)據(jù)庫系統(tǒng)都不支持 TOP 子句。例如,MySQL 支持 LIMIT 子句,用以取回有限數(shù)量的記錄,而 Oracle 則使用 ROWNUM 子句來實現(xiàn)這一功能。
在 SELECT 語句中使用 TOP 子句的基本語法如下所示:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE [condition]
考慮含有如下所示記錄的 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 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
下面的例子將從 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 數(shù)據(jù)庫服務器,那么等價的例子如下所示:
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 數(shù)據(jù)庫服務器,那么等價的例子如下所示:
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 |
+----+---------+-----+-----------+---------+
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: