dsigh7478 2014-05-14 20:41
浏览 63
已采纳

论坛 - 由多个页面上的最后一个活动排序的主题

I am currently creating a forum with php and sql. My problem is how to get the topics into the right order assuming that there are a total of 200 and 15 per Page and I'm on page number 10.

I can't select per id because if someone posts into one of these topics the timestamp of that specific topic gets updated -> the newest on top.

For the first page something like that could be okay:

select * from topics order by time desc limit 15 

but for the next page I would need the timestamp of the last topic on the page before.

If I could select a specific table by index relative to the timestamp order without the need for the actual index.

  • 写回答

1条回答 默认 最新

  • duanlie7447 2014-05-14 21:12
    关注

    I suggest you check the MySQL documentations for the second argument of the LIMIT clause:

    With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1)

    Syntax:

    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    

    Example:

    -- page 2:
    SELECT * FROM topics ORDER BY time DESC LIMIT 15, 15;
    SELECT * FROM topics ORDER BY time DESC LIMIT 15 OFFSET 15;
    
    -- page 3:
    SELECT * FROM topics ORDER BY time DESC LIMIT 30, 15;
    SELECT * FROM topics ORDER BY time DESC LIMIT 15 OFFSET 30;
    
    -- page n:
    SELECT * FROM topics ORDER BY time DESC LIMIT (n-1)*15, 15;
    SELECT * FROM topics ORDER BY time DESC LIMIT 15 OFFSET (n-1)*15;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大