繁体   English   中英

select 如何在 sql 中进行下一行分页

[英]How select next row pagination in sql

对不起,我的英语很弱。 我在每页中回显 2 行。 如何回显下 2 行

SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category 
WHERE mzmx_post.id = mzmx_post_category.post_id AND zmx_post_category.category_id = 5
ORDER BY id DESC 
LIMIT 2

您可以使用LIMIT的两个参数形式将结果偏移给定的行数,例如:

SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category ON mzmx_post.id = mzmx_post_category.post_id 
WHERE mzmx_post_category.category_id = 5
ORDER BY id DESC
LIMIT 2, 2      -- fetch records 3 and 4

这为您提供了第二页。 如果你想要第三页,那么:

LIMIT 4, 2

等等。

请注意,我修改了您的查询,因此表之间的连接条件放在连接的ON子句中,而不是WHERE子句中。

基本思想是使用

LIMIT n,o

在哪里
n 是每页的结果
o 是与第一个结果的偏移量

对于第 p 页,偏移量为

o = p * n
其中 p = 0,1,2,....

最好在每个表中添加一个 Long 类型的额外列(例如 mzmx_post_key bigint),并在该列上具有顺序值。 使用该列从分页的数据库中获取数据。 sqL suery 应该如下所示:

SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category ON mzmx_post.id = mzmx_post_category.post_id 
WHERE mzmx_post_category.category_id = 5 and mzmx_post_key> ##last record key##
ORDER BY mzmx_post_key ASC
LIMIT 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM