gpt4 book ai didi

php - 论坛 - 按上次事件在多个页面上排序的主题

转载 作者:行者123 更新时间:2023-11-29 12:58:39 24 4
gpt4 key购买 nike

我目前正在使用 php 和 sql 创建一个论坛。我的问题是如何将主题按正确的顺序排列,假设每页总共有 200 和 15 个主题,而我在第 10 页上。

我无法选择每个 ID,因为如果有人将时间戳发布到这些主题之一该特定主题的内容已更新 -> 最新的位于顶部。

对于第一页,类似这样的内容可能没问题:

select * from topics order by time desc limit 15 

但对于下一页,我需要之前页面上最后一个主题的时间戳。

如果我可以通过相对于时间戳顺序的索引选择特定表,而不需要实际索引。

最佳答案

我建议您检查MySQL documentations对于 LIMIT 子句的第二个参数:

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)

语法:

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

示例:

-- 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;

关于php - 论坛 - 按上次事件在多个页面上排序的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23664715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com