MySQL线性MIN MAX插值提取

时间:2015-11-13 04:52:16

标签: mysql interpolation

我想用BASh脚本进行插值。我有一个数据库,[从一个片段]看起来像这样:

enter image description here

对于给定值 timestamp ,我希望SQL返回最近的 timestamp 更大的值,并带有相应的value和类似于 timestamp 的最接近的较小值也返回相应的value

问:查询提取此信息的样子是什么?

1 个答案:

答案 0 :(得分:0)

你可以试试这个。另请注意,时间戳为reserved word for MySQL

SELECT (
    SELECT `timestamp` 
    FROM your_table 
    WHERE `timestamp` > 'your_value' 
    ORDER BY `timestamp` ASC 
    LIMIT 1
) AS one_greater,
(
    SELECT `timestamp` 
    FROM your_table 
    WHERE `timestamp` < 'your_value' 
    ORDER BY `timestamp` DESC 
    LIMIT 1
) AS one_less;