如何在mysql中的两个日期范围内获取每天的每小时数据?

时间:2018-08-17 09:46:29

标签: mysql sql

在我的查询下面:

SELECT DATE_FORMAT( FROM_UNIXTIME(  `temp_timing` ) ,  '%Y-%m-%d %H:%i:%s' ) AS timing, temperature, deviceid
FROM tempdata
WHERE deviceid =29
AND DATE_FORMAT( FROM_UNIXTIME(  `temp_timing` ) ,  '%Y-%m-%d' ) >=  '2018-07-20'
AND DATE_FORMAT( FROM_UNIXTIME(  `temp_timing` ) ,  '%Y-%m-%d' ) <=  '2018-07-27'
ORDER BY timing ASC 
LIMIT 0 , 30

1 个答案:

答案 0 :(得分:0)

可以通过以下查询

SELECT DATE_FORMAT( FROM_UNIXTIME(  `temp_timing` ) ,  '%Y-%m-%d %H:%i:%s' ) AS timing, AVG( temperature ) AS avg, deviceid
FROM tempdata
WHERE deviceid =29
AND DATE_FORMAT( FROM_UNIXTIME(  `temp_timing` ) ,  '%Y-%m-%d' ) >=  '2018-07-20'
AND DATE_FORMAT( FROM_UNIXTIME(  `temp_timing` ) ,  '%Y-%m-%d' ) <=  '2018-07-27'
GROUP BY DATE( timing ) , HOUR( timing ) 
ORDER BY  `timing` ASC 
LIMIT 0 , 30