简体   繁体   English

时间间隔内的SQL

[英]SQL within time interval

I have the below query which will query from a date range and fixed timing on the appointed devices. 我有以下查询,它将查询指定设备上的日期范围和固定时间。 How could I modify the query so that the time within a + - 10minutes. 我如何修改查询,使时间在+-10分钟内。 For example if user chosen 4:00pm and the query shall cater for 3:50pm until 4:10pm on every single day within the date range. 例如,如果用户选择了4:00 pm,并且查询应在日期范围内的每一天满足下午3:50,直到下午4:10。

SELECT Timestamp, AVG(Value) 
FROM STATIC_SENSOR_READINGS 
WHERE 
Node_ID IN ($sdNodeList) AND  
Modality = 'Temperature' AND 
Timestamp BETWEEN ? AND ? AND  // This will be the date range.
date_format(`Timestamp`, '%H:%i') = '$time'  // Specific time here
GROUP BY Timestamp

To check timestamps are "approximately" equal in mysql, you can use the TIMESTAMPDIFF() function. 要检查时间戳在mysql中“大约”相等,可以使用TIMESTAMPDIFF()函数。

In your example, you need something like: 在您的示例中,您需要以下内容:

SELECT Timestamp, AVG(Value) 
FROM STATIC_SENSOR_READINGS 
WHERE 
Node_ID IN ($sdNodeList) AND  
Modality = 'Temperature' AND 
Timestamp BETWEEN ? AND ? AND  // This will be the date range.
ABS(TIMESTAMPDIFF(MINUTES, Timestamp, '$time')) < 10 
GROUP BY Timestamp

You may have to adjust time/timestamp formats to suit your needs. 您可能需要调整时间/时间戳格式以适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM