PostgreSQL 中的 limit 子句用于限制 SELECT 语句中查询的数据的数量。
SELECT * FROM "TB01" LIMIT [no of rows]
select * from class_name LIMIT 3;
SELECT * FROM table_name
LIMIT [pageSize] OFFSET [pageNum]
-- pageSize:每页显示条数
-- pageNum:从第几条开始查询
表内容:
同上
执行sql(sql中注释解释了分页逻辑):
--SELECT * FROM table_name LIMIT [pageSize] OFFSET [pageNum]
-- pageSize:每页显示条数
-- pageNum:从第几条开始查询
-- - pageNum= (pageSize-1)*页数
--第一页 pageSize:3 pageNum:(1-1)*3=0
select * from class_name LIMIT 3 OFFSET 0;
--第二页 pageSize:3 pageNum:(2-1)*3=3
select * from class_name LIMIT 3 OFFSET 3;
--第三页 pageSize:3 pageNum:(3-1)*3=6
select * from class_name LIMIT 3 OFFSET 6;
-- 。。。。。。。。。。。。。。。。。。。。。。。。。
如需要表结构和测试数据,直接复制下面的数据即可。
表结构
CREATE TABLE "public"."class_name" (
"id" int4 NOT NULL DEFAULT nextval('id'::regclass),
"class_no" varchar(50) COLLATE "pg_catalog"."default",
"name" varchar(255) COLLATE "pg_catalog"."default",
"age" int4,
CONSTRAINT "class_name_pkey" PRIMARY KEY ("id")
)
;
ALTER TABLE "public"."class_name"
OWNER TO "postgres";
数据:
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (1, '1', '二哈', 2);
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (2, '1', '金毛', 3);
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (3, '1', '拉布', 4);
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (4, '2', '折耳', 5);
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (5, '2', '波斯', 5);
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (6, '3', '杰克', 6);
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (7, '3', '肉丝', 33);
INSERT INTO "public"."class_name"("id", "class_no", "name", "age") VALUES (8, '3', '杰克', 7);