我使用unix时间戳每隔1-3秒记录一次温度,我希望每1分钟平均返回一行数据并持续24小时。
我的记录器表如下所示:
unixtime temp
1350899052 25.37
1350899054 25.44
1350899057 25.44
1350899059 25.44
1350899062 25.44
1350899064 25.44
1350899069 25.44
1350899071 25.44
我希望它像
一样返回unixtime temp
1350899052 25.37 -- average value of 1 minute range both unixtime and temp
1350899054 25.44
1350899057 25.44
请告诉我应该怎么做mySQL命令?感谢。
答案 0 :(得分:1)
以下内容应该有效:
SELECT ROUND(unixtime/(60)) AS minute, avg(temp)
FROM table where unixtime >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY ROUND(unixtime/(60));
以正常格式显示日期:
SELECT from_unixtime(floor(unixtime/(60))*60) AS minute, avg(temp) FROM test where unixtime >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY floor(unixtime/(60))
输出:
minute avg(temp)
2012-10-24 08:02:00 37.5
2012-10-24 08:03:00 45
答案 1 :(得分:0)
1. 1 min
select * from logger_table where unixtime> timestamp(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) group by temp
2. 24 hrs
select * from logger_table where unixtime> timestamp(DATE_SUB(NOW(), INTERVAL 24 HOUR)) group by temp;