SQL LAST() 函數(shù)


 LAST() 函數(shù)返回指定的列中最后一個記錄的值。

SQL LAST() 語法

SELECT LAST(column_name) FROM table_name;    

注釋:只有 MS Access 支持 LAST() 函數(shù)。


SQL Server、MySQL 和 Oracle 中的 SQL LAST() 工作區(qū)


SQL Server 語法

SELECT TOP 1 column_name
FROM table_name
ORDER BY column_name DESC;        

實例

SELECT TOP 1 CustomerName FROM Customers               
ORDER BY CustomerID DESC;        

MySQL 語法

SELECT column_name FROM table_name                
ORDER BY column_name DESC                
LIMIT 1;        

實例

SELECT CustomerName FROM Customers                
ORDER BY CustomerID DESC                
LIMIT 1;     

Oracle 語法

SELECT column_name FROM table_name               
ORDER BY column_name DESC                
WHERE ROWNUM <=1;       

實例

SELECT CustomerName FROM Customers                
ORDER BY CustomerID DESC                
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 LAST() Example


 下面的 SQL 語句選取 "Customers" 表的 "CustomerName" 列中最后一個記錄的值:

實例

SELECT LAST(CustomerName) AS LastCustomer FROM Customers;