简体   繁体   English

调用MySQL查询中的下一条和上一条记录

[英]Call the next and previous records in a MySQL query

How do I call the next and previous MySQL records, without referring to the ID? 如何在不参考ID的情况下调用下一个和上一个MySQL记录?

In my table, I have the following records: 在我的表中,我有以下记录:

  • IDNO (Auto-increment, primary key) IDNO(自动递增,主键)
  • Title 标题
  • Slug (unique key) 弹头(唯一键)
  • Data 数据
  • DateTime (Ymd H:i:s) 日期时间(Ymd H:i:s)

I have two pages - index.php and single.php. 我有两个页面-index.php和single.php。 In index.php, my posts are called (10 at a time) and ordered by DateTime . 在index.php中,我的帖子被称为(一次10个)并由DateTime排序。 Each entry links to single.php, where the single post is displayed, based on the URL variable slug and calling the appropriate record in my database. 每个条目都基于URL变量slug链接到single.php,并在其中显示单个帖子,并调用数据库中的相应记录。 I would like, since the pages are making up a simple blog, to call the next post and the previous post, still in DateTime order (the next will link to the more recent post, the previous link to the less recent post). 我想,由于这些页面构成了一个简单的博客,因此仍按DateTime顺序调用下一个帖子和上一个帖子(下一个链接将链接到最新的帖子,上一个链接将链接到较新的帖子)。 Any advice on how to do this? 有关如何执行此操作的任何建议? If it was based on IDNO, I could do it, but since it isn't and the posts are not naturally in DateTime order, but are ordered as such in my query, I am at a loss. 如果它是基于IDNO的,我可以这样做,但是由于它不是,并且帖子不是自然地按DateTime顺序排列的,而是按我的查询顺序进行排列,所以我很茫然。

The query I use on each single.php page is: 我在每个single.php页面上使用的查询是:

SELECT * FROM site_posts WHERE slug = '".$_GET['SLUG']."'

I want the links to appear on this page, so I am only including that query. 我希望链接显示在此页面上,所以只包括该查询。

For the next post 对于下一篇文章

SELECT * FROM site_posts WHERE DateTime > $currentPostDateTime   
  ORDER BY DateTime ASC LIMIT 0,1  

and
for the previous post 对于上一篇文章

SELECT * FROM site_posts WHERE DateTime < $currentPostDateTime   
  ORDER BY DateTime DESC LIMIT 0,1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM