简体   繁体   English

如何基于时间戳获取mysql中的下一个/上一个记录

[英]how to get next/previous records in mysql based on timestamp

I was wondering how to select next and previous rows from a mysql database with reference to my currently selected row. 我想知道如何参考我当前选择的行从mysql数据库中选择下一行和上一行。

I found this question on SO How to get next/previous record in MySQL? 我在SO上发现了这个问题。 如何在MySQL中获取下一个/上一个记录? but in this case the select is done based on higher and lower id from the reference. 但是在这种情况下,选择是根据参考中的较高和较低ID进行的。 I would like to use earlier or later timestamp instead of ids. 我想使用更早或更晚的时间戳而不是id。

There is no reason for using subqueries. 没有理由使用子查询。

Next: 下一个:

SELECT * FROM `my_table`
WHERE `the_timestamp` > 123456
ORDER BY `the_timestamp` ASC
LIMIT 1

Prev: 上一条:

SELECT * FROM `my_table`
WHERE `the_timestamp` < 123456
ORDER BY `the_timestamp` DESC
LIMIT 1

Based on the article you linked to, you can use: 根据您链接到的文章,您可以使用:

SELECT *
FROM `my_table`
WHERE `the_timestamp` = (
    SELECT MIN(`the_timestamp`)
    FROM `my_table`
    WHERE `the_timestamp` > 1373493634
);

It's formatted for readability. 对其进行格式化以提高可读性。 Replace the timestamp I used (1373493634) with the timestamp you want to find the next record after. 将我使用的时间戳(1373493634)替换为要在其后查找下一个记录的时间戳。

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

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