我有两张这样的桌子:
温度 :
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| date | datetime | YES | UNI | NULL | |
| capteur | int(11) | YES | | NULL | |
| valeur | float(3,1) | YES | | NULL | |
+---------+---------------------+------+-----+---------+----------------+
加湿:
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| date | datetime | YES | UNI | NULL | |
| capteur | int(11) | YES | | NULL | |
| valeur | int(11) | YES | | NULL | |
+---------+---------------------+------+-----+---------+----------------+
我在这两个表上记录了值,而不是同时记录(每分钟大约 1 条记录)。
如果我输入这个命令,我会得到过去 24 小时每小时的平均值(所以,24 行):
$sql->query('SELECT hour(date) AS humhour,ROUND(AVG(valeur),1) AS avghum FROM humidite WHERE date >= (now() - INTERVAL 1 DAY) GROUP BY HOUR(date) ORDER BY DATE;');
现在,我尝试获得相同的东西,但使用两张表。即,对于 0h00 和 0h59 之间的所有值,我想要所有温度的平均值和所有湿度值的平均值。
我试试这个命令:
$result = $sql->query('
SELECT hour(temperature.date) AS hourtemp,
hour(humidite.date) AS hourhum,
ROUND(AVG(temperature.valeur),1) AS avgtemp,
ROUND(AVG(humidite.valeur),1) AS avghum
FROM temperature
INNER JOIN humidite on hour(temperature.date) = hour(humidite.date)
WHERE temperature.date >= (now() - INTERVAL 1 DAY)
GROUP BY HOUR(date)
ORDER BY DATE;');
一个主意 ?
谢谢 !