【问题标题】:How select next row pagination in sql如何在sql中选择下一行分页
【发布时间】:2023-03-17 17:17:01
【问题描述】:

对不起,我的英语很差。 我在每页中回显 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

【问题讨论】:

    标签: php mysql sql select sql-order-by


    【解决方案1】:

    您可以使用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 子句中。

    【讨论】:

      【解决方案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
      

      【讨论】:

        【解决方案3】:

        基本思路是使用

        LIMIT n,o
        

        其中
        n 是每页的结果
        o 是与第一个结果的偏移量

        对于第 p 个页面,偏移量为

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-29
          相关资源
          最近更新 更多