0

我在离散的时间点有一组位置。我想使用一个函数来返回具有第一个较小和第一个较高时间戳的位置。

例如

timestamp | pos
5         | (-3, -3)
10        | (0, 0)
15        | (3, 3)
20        | (6, 6)

如果我在这个表中查询时间戳 17,我会得到行

15        | (3, 3)
20        | (6, 6)

我期待类似的东西

SELECT * FROM positions WHERE timestamp = first_smaller(17)
SELECT * FROM positions WHERE timestamp = first_greater(17)
4

2 回答 2

3
select * from positions where timestamp <= 17 order by timestamp desc limit 1;

select * from positions where timestamp >= 17 order by timestamp asc limit 1;
于 2013-07-03T14:20:09.177 回答
0

像这样的东西应该工作:

(SELECT * 
 FROM positions 
 WHERE timestamp = (SELECT max(timestamp) FROM position WHERE timestamp <= 17))
UNION
(SELECT * 
 FROM positions 
 WHERE timestamp = (SELECT min(timestamp) FROM position WHERE timestamp >= 17))
于 2013-07-03T14:20:32.950 回答