因此,例如,如果我有两个表communication
和movement
都具有名为ID
和timestamp
的列,并且我有一个查询,通常看起来像这样:
SELECT * FROM movement m
JOIN communication c
ON m.ID = c.ID
WHERE m.timestamp <= c.timestamp
ORDER BY abs(TIMESTAMPDIFF(second,ctstamp,m.timestamp))
LIMIT 1;
当
communication
时,即movement
小于m.timestamp <= c.timestamp
时,它将找到给定movement timestamp
时间戳的最接近的communication timestamp
时间戳。但这省去了所有m.timestamp >= c.timestamp
。我想做的是在任一侧创建一个大约10秒的范围,这样它将是:
WHERE m.timestamp BETWEEN c.timestamp-10 secs AND c.timestamp+10 secs
因此,如果
communication timestamp
是'2012-03-02|09:02:30'
,则它将是:WHERE m.timestamp BETWEEN '2012-03-02|09:02:20' AND '2012-03-02|09:02:40'
那么我将如何实现呢?
任何帮助将不胜感激,谢谢!
编辑
最后,我使用了这个:
WHERE m.timestamp BETWEEN TIMESTAMPADD(SECOND,-10,c.timestamp)
AND TIMESTAMPADD(SECOND,10,c.timestamp);
最佳答案
只需使用interval
关键字:
WHERE m.timestamp BETWEEN c.timestamp - interval 10 second AND
c.timestamp + interval 10 second
关于mysql - SQL-如何使用BETWEEN(mysql)获取一系列时间戳?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30790020/