猿问

如何在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


ibeautiful
浏览 123回答 3
3回答

千巷猫影

您可以使用双参数形式LIMIT将结果偏移给定的行数,例如:SELECT *FROM `mzmx_post`JOIN mzmx_post_category ON mzmx_post.id = mzmx_post_category.post_id WHERE mzmx_post_category.category_id = 5ORDER BY id DESCLIMIT 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,....

慕哥6287543

最好在每个表中添加一个 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 ASCLIMIT 2
随时随地看视频慕课网APP
我要回答