请稍候,加载中....

SQL FIRST() 函数

FIRST() 函数

FIRST() 函数返回指定的列中第一个记录的值。

SQL FIRST() 语法

SELECT FIRST(column_name) FROM table_name;

注释:只有 MS Access 支持 FIRST() 函数。

 


SQL Server 语法

SELECT TOP 1 column_name FROM table_name
ORDER BY column_name ASC;

SQL SERVER top查询示例

SELECT TOP 1 order_id FROM orders
ORDER BY order_amount ASC;

 


MySQL 语法

SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;

MySQL limit查询示例

SELECT order_id FROM orders
ORDER BY order_amount ASC
LIMIT 1;

 


Oracle 语法

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

Oracle rownum查询示例

SELECT name FROM Websites
ORDER BY id ASC
WHERE ROWNUM <=1;

 


演示数据库

在本教程中,我们将使用 yuanbian 样本数据库。

下面是选自 "orders" 表的数据:

+----------+--------------+----------+--------------+----------------+---------------------+
| order_id | order_no     | goods_id | order_amount | order_quantity | create_date         |
+----------+--------------+----------+--------------+----------------+---------------------+
|        1 | 201503120001 |       10 |         1000 |             10 | 2015-03-12 09:00:04 |
|        2 | 201503120002 |       12 |         1300 |             12 | 2015-03-12 17:23:10 |
|        3 | 201503130001 |       11 |         2400 |             22 | 2015-03-13 10:30:12 |
|        4 | 201503130002 |       12 |         2500 |             15 | 2015-03-13 11:00:05 |
|        5 | 201503130003 |       10 |         1400 |             15 | 2015-03-13 14:54:00 |
|        6 | 201503140001 |       12 |         2100 |             12 | 2015-03-14 11:34:00 |
+----------+--------------+----------+--------------+----------------+---------------------+

 


SQL FIRST() 示例

下面的 SQL 语句选取 "orders" 表的 "order_amount" 金额最大的一条的记录:

SELECT *
FROM orders
ORDER BY order_amount
LIMIT 1;

执行以上 SQL 结果如下所示:

+----------+--------------+----------+--------------+----------------+---------------------+
| order_id | order_no     | goods_id | order_amount | order_quantity | create_date         |
+----------+--------------+----------+--------------+----------------+---------------------+
|        1 | 201503120001 |       10 |         1000 |             10 | 2015-03-12 09:00:04 |
+----------+--------------+----------+--------------+----------------+---------------------+

Python学习手册-