SQLite的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表中有以下记录:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
限制从表中提取的行数:
sqlite> SELECT * FROM COMPANY LIMIT 6;
结果:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
但是,在某些情况下,可能需要从一个特定的偏移开始提取记录。下面是一个实例,第三位开始提取3个记录:
sqlite> SELECT * FROM COMPANY LIMIT 3 OFFSET 2;
结果:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
参考:
https://www.yuque.com/docs/share/1f9341b3-e09c-48e9-8c96-f79fd2b9e807