简体   繁体   English

MySQL的平均温度读数除以小时

[英]Mysql Php Average of temperature reads divided by hour

I have a MYSQL DB with two columns, one is the temperature and the other one is the time in which the temperatura was read, like http://pastebin.mozilla.org/1860064 . 我有一个包含两列的MYSQL DB,一列是温度,另一列是读取温度的时间,例如http://pastebin.mozilla.org/1860064 The temperature USUALLY have a 5minutes difference between reads, but this is not always with this time gap. 两次读取之间的温度通常相差5分钟,但这并不总是有这个时间间隔。

What I need to figure out is a algorithm to read all the values and save the average of every hour of read, so temperature will be saved on the db with one temperature per hour, 24 reads per day, and it would save in another db's table. 我需要弄清楚的是一种算法,它可以读取所有值并保存每小时读取的平均值,因此温度将以每小时一个温度,每天24次读取的方式保存在db上,并将其保存在另一个db中表。

For example: 例如:

在此处输入图片说明

It would save with the average temperature and with the date 2012-10-07 10:00:00, 可以节省平均温度和日期2012-10-07 10:00:00,

Was I clear? 我说清楚了吗

Here is one way to do this with just a query. 这是仅通过查询执行此操作的一种方法。 If you need the results stored in the database you'll need your own inserts but this will get you the data you're looking for. 如果需要将结果存储在数据库中,则需要自己插入,但这将为您提供所需的数据。

SELECT AVG(temperature),HOUR(read_time) as read_hour FROM temperature_reads WHERE DATE(read_time) = '2012-10-06' GROUP BY read_hour

You can swap the 2012-10-06 part out for whatever date you are looking at. 您可以将2012-10-06部分换成您想要的日期。

Edit: Here it is updated with your info: 编辑:在这里它已更新为您的信息:

SELECT AVG(temperatura),HOUR(data) as read_hour FROM leituras WHERE DATE(read_time) = '2012-10-06' GROUP BY read_hour

This should do the trick! 这应该可以解决问题!

Another way to do this is without filtering any day and including the whole day and hour on the grouping clause: 执行此操作的另一种方法是不过滤任何一天,并且不包括分组子句中的整天和整小时:

SELECT AVG(temperature),
       DATE_FORMAT(date, '%d/%m/%Y %H') day_hour_read
  FROM TABLE
GROUP BY DATE_FORMAT(date, '%d/%m/%Y %H')

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

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