1

我有 2 个表,每个表都有一个时间戳列。如何查询 A 中的每一行,B 中的第一个前后时间戳?

我想:

A.id A.timestamp first_preceding(B.timestamp) first_following(B.timestamp)

4

1 回答 1

2

我会试试这个:

SELECT DISTINCT a.id, a.timestamp, b0.timestamp, b1.timestamp
FROM a, b b0, b b1
WHERE
b0.timestamp = (SELECT MAX(timestamp) FROM b WHERE timestamp < a.timestamp)
AND b1.timestamp = (SELECT MIN(timestamp) FROM b WHERE timestamp > a.timestamp);
于 2012-11-02T03:00:17.383 回答