i有2个表,每个表都有一个时间戳列。我如何查询A中的每一行,B?
中的第一个和之后的时间戳我想要:
a.id a.timestamp first_preceding(b.timestamp)first_following(b.timestamp)
我会尝试的:
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);