扫码一下
查看教程更方便
PostgreSQL LIMIT子句用于限制SELECT 语句返回的数据量。
带有 LIMIT 子句的 SELECT 语句的基本语法如下:
SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows]
下面是 LIMIT 子句与 OFFSET 子句一起使用时的语法:
SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows] OFFSET [row num]
创建 COMPANY 表(下载 COMPANY SQL 文件 ),数据内容如下:
jiyik_db=# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
(7 rows)
下面示例将找出限定的数量的数据,即读取 4 条数据:
jiyik_db=# SELECT * FROM COMPANY LIMIT 4;
结果如下:
id | name | age | address | salary
----+-------+-----+-------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
(4 rows)
但是,在某些情况下,可能需要从一个特定的偏移开始提取记录。
下面是一个实例,从第三位开始提取 3 个记录:
jiyik_db=# SELECT * FROM COMPANY LIMIT 3 OFFSET 2;
结果如下:
id | name | age | address | salary
----+-------+-----+-----------+--------
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
(3 rows)