PHP从MySQL获得下一个/上一个记录

时间:2014-11-25 22:42:37

标签: php mysql

我在一个像博客一样工作的网站上工作。在这篇博客中,有文章(我将它们添加到另一个管理员),这些文章有部分。我想要的是,当用户正在阅读帖子上方的任何帖子时,会有2个按钮:previous articlenext article。有MySQL数据库,看起来像这样(parentid部分):

+------+-------+---------+--------+
| id   | title | content | parent | etc...
+------+-------+---------+--------+
| 1    | test1 | ...     | 2      |
| 2    | test2 | ...     | 4      |
| 3    | test3 | ...     | 2      |
| 4    | test4 | ...     | 1      |
| 5    | test5 | ...     | 2      |
+------+-------+---------+--------+

现在,例如,用户正在阅读test3并且我想要,它会返回我{ids 15,因为test1是以前的文章相同{ {1}}和parent是下一篇文章,内容相同test5

我该怎么做才能得到这样的结果?

1 个答案:

答案 0 :(得分:0)

上一篇文章的查询:

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 
相关问题