SQL FIRST() 函數(shù)
FIRST() 函數(shù)返回指定的列中第一個記錄的值。
SQL FIRST() 語法
SELECT FIRST(column_name) FROM table_name;
注釋:只有 MS Access 支持 FIRST() 函數(shù)。
SQL Server、MySQL 和 Oracle 中的 SQL FIRST() 工作區(qū)
SQL Server 語法
SELECT TOP 1 column_name
FROM table_name
ORDER BY column_name ASC;
實例
SELECT TOP 1 CustomerName FROM Customers
ORDER BY CustomerID ASC;
MySQL 語法
SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;
實例
SELECT CustomerName FROM Customers
ORDER BY CustomerID ASC
LIMIT 1;
Oracle 語法
SELECT column_name FROM table_name
ORDER BY
column_name ASC
WHERE ROWNUM <=1;
實例
SELECT CustomerName FROM Customers
ORDER BY CustomerID ASC
WHERE ROWNUM <=1;
演示數(shù)據(jù)庫
在本教程中,我們將使用眾所周知的 Northwind 樣本數(shù)據(jù)庫。
下面是選自 "Customers" 表的數(shù)據(jù):
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1
|
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
4
|
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbk?p | Christina Berglund | Berguvsv?gen 8 | Lule? | S-958 22 | Sweden |
SQL FIRST() 實例
下面的 SQL 語句選取 "Customers" 表的 "CustomerName" 列中第一個記錄的值:
實例
SELECT FIRST(CustomerName) AS FirstCustomer FROM Customers;
更多建議: