我在做一个网站,它就像一个博客。在这个博客中,有一些文章(我将它们添加到另一个管理员那里),这些文章有部分。我希望,当用户阅读任何帖子时,帖子上方将有 2 个按钮:previous article
和 next article
。有MySQL数据库,看起来像这样(parent
是本节id
):
+------+-------+---------+--------+
| id | title | content | parent | etc...
+------+-------+---------+--------+
| 1 | test1 | ... | 2 |
| 2 | test2 | ... | 4 |
| 3 | test3 | ... | 2 |
| 4 | test4 | ... | 1 |
| 5 | test5 | ... | 2 |
+------+-------+---------+--------+
现在,例如,用户正在阅读test3
,我希望它将返回我的 ids 1
和 5
,因为test1
是具有相同parent
的上一篇文章,而test5
是具有相同parent
的下一篇文章。
我该怎么办才能得到这样的结果?
上一篇文章的查询:
SELECT id FROM article WHERE id < currentID AND parent = 2 ORDER BY id DESC LIMIT 1
下一篇文章的查询:
SELECT id FROM article WHERE id > currentID AND parent = 2 ORDER BY id ASC LIMIT 1