我記錄從溫度和溼度傳感器的值,這樣的兩個表:從this post我用這個MySQL的命令來連接兩個表,並得到一個值Mysql的JOIN基於異步時間和平均值
mysql> SELECT date,valeur FROM temperature WHERE date >= NOW() - INTERVAL 2 HOUR;
+---------------------+--------+
| date | valeur |
+---------------------+--------+
| 2013-09-26 11:30:40 | 25.2 |
| 2013-09-26 11:33:19 | 25.4 |
| 2013-09-26 11:34:12 | 25.5 |
| 2013-09-26 11:38:37 | 25.4 |
| 2013-09-26 11:39:30 | 25.4 |
| 2013-09-26 11:40:23 | 25.4 |
| 2013-09-26 11:43:02 | 25.4 |
| 2013-09-26 11:45:41 | 25.3 |
| 2013-09-26 11:47:33 | 25.3 |
| 2013-09-26 11:51:07 | 25.4 |
| 2013-09-26 11:51:52 | 25.3 |
| 2013-09-26 11:53:38 | 25.4 |
...
| 2013-09-26 12:26:20 | 25.5 |
| 2013-09-26 12:27:12 | 25.5 |
| 2013-09-26 12:32:30 | 25.4 |
| 2013-09-26 12:35:09 | 25.5 |
| 2013-09-26 12:36:02 | 25.5 |
| 2013-09-26 12:37:50 | 25.6 |
| 2013-09-26 12:40:27 | 25.6 |
| 2013-09-26 12:49:18 | 25.6 |
| 2013-09-26 12:52:54 | 25.6 |
...
| 2013-09-26 13:16:40 | 25.5 |
| 2013-09-26 13:17:33 | 25.6 |
| 2013-09-26 13:22:05 | 25.5 |
| 2013-09-26 13:22:51 | 25.5 |
| 2013-09-26 13:23:44 | 25.5 |
| 2013-09-26 13:24:37 | 25.5 |
| 2013-09-26 13:25:30 | 25.5 |
| 2013-09-26 13:28:09 | 25.6 |
+---------------------+--------+
mysql> SELECT date,valeur FROM humidite WHERE date >= NOW() - INTERVAL 2 HOUR;
+---------------------+--------+
| date | valeur |
+---------------------+--------+
| 2013-09-26 11:30:40 | 78 |
| 2013-09-26 11:33:19 | 78 |
| 2013-09-26 11:34:12 | 78 |
| 2013-09-26 11:38:37 | 78 |
| 2013-09-26 11:39:30 | 78 |
| 2013-09-26 11:40:23 | 78 |
| 2013-09-26 11:43:02 | 79 |
| 2013-09-26 11:45:41 | 78 |
| 2013-09-26 11:47:33 | 78 |
| 2013-09-26 11:51:07 | 79 |
| 2013-09-26 11:51:52 | 79 |
| 2013-09-26 11:53:38 | 79 |
...
| 2013-09-26 12:22:48 | 78 |
| 2013-09-26 12:26:20 | 78 |
| 2013-09-26 12:27:12 | 78 |
| 2013-09-26 12:32:30 | 77 |
| 2013-09-26 12:35:09 | 77 |
| 2013-09-26 12:36:02 | 77 |
| 2013-09-26 12:37:50 | 78 |
| 2013-09-26 12:40:27 | 77 |
| 2013-09-26 12:43:06 | 78 |
| 2013-09-26 13:22:05 | 78 |
...
| 2013-09-26 13:22:51 | 78 |
| 2013-09-26 13:23:44 | 78 |
| 2013-09-26 13:24:37 | 78 |
| 2013-09-26 13:25:30 | 78 |
| 2013-09-26 13:28:09 | 78 |
+---------------------+--------+
因爲每24小時小時:
SELECT DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i') date,
T2.valeur temp,
P2.valeur hum
FROM
(
SELECT minuteTime.minuteTime minuteTime,
(SELECT MAX(date) FROM temperature WHERE date <= minuteTime.minuteTime AND date >= NOW() - INTERVAL 1 DAY) tempTime,
(SELECT MAX(date) FROM humidite WHERE date <= minuteTime.minuteTime AND date >= NOW() - INTERVAL 1 DAY) humTime
FROM
(
SELECT DATE(date) + INTERVAL (HOUR(date) DIV 1 * 1) HOUR minuteTime
FROM humidite
WHERE date >= NOW() - INTERVAL 1 DAY AND date <= NOW()
UNION SELECT DATE(date) + INTERVAL (HOUR(date) DIV 1 * 1) HOUR
FROM temperature
WHERE date >= NOW() - INTERVAL 1 DAY AND date <= NOW()
GROUP BY 1
) minuteTime
) timeTable
LEFT JOIN temperature T2 ON T2.date = timeTable.tempTime
LEFT JOIN humidite P2 ON P2.date = timeTable.humTime
ORDER BY minuteTime ASC;
它運作良好,但我真的不知道該值是如何選擇(隨機在區間?)。我寧願平均每個小時,而不是:
+------------------+------+------+
| date | temp | hum |
+------------------+------+------+
| 2013-09-25 13:00 | NULL | NULL |
| 2013-09-25 14:00 | 25.1 | 80 |
| 2013-09-25 15:00 | 25.3 | 78 |
| 2013-09-25 16:00 | 25.6 | 75 |
| 2013-09-25 17:00 | 25.5 | 75 |
| 2013-09-25 18:00 | 25.0 | 78 |
| 2013-09-25 19:00 | 24.2 | 80 |
| 2013-09-25 20:00 | 23.9 | 84 |
| 2013-09-25 21:00 | 23.9 | 84 |
| 2013-09-25 22:00 | 24.1 | 83 |
| 2013-09-25 23:00 | 24.1 | 83 |
| 2013-09-26 0:00 | 24.0 | 82 |
| 2013-09-26 1:00 | 23.9 | 84 |
| 2013-09-26 2:00 | 23.7 | 86 |
| 2013-09-26 3:00 | 23.7 | 84 |
| 2013-09-26 4:00 | 23.7 | 85 |
| 2013-09-26 5:00 | 23.4 | 85 |
| 2013-09-26 6:00 | 23.6 | 85 |
| 2013-09-26 7:00 | 23.7 | 85 |
| 2013-09-26 8:00 | 24.0 | 84 |
| 2013-09-26 9:00 | 24.4 | 82 |
| 2013-09-26 10:00 | 24.6 | 81 |
| 2013-09-26 11:00 | 25.2 | 79 |
| 2013-09-26 12:00 | 25.4 | 79 |
| 2013-09-26 13:00 | 25.6 | 78 |
+------------------+------+------+
25 rows in set (1.11 sec)
你知道如何把AVG()在此代碼?
獎金問題:我改變了代碼,以獲得每小時一個值。你知道如何每30分鐘獲得一個值嗎?
感謝您的幫助
感謝湯姆的Mac您的回覆。平均值似乎表現良好,但產出仍然是一小時一小時,而不是每30分鐘一次。也許我需要改變日期(日期)+ INTERVAL HOUR(日期)HOUR分鐘時間由別的東西? – iero
我再次測試,平均值給出了奇怪的值:溫度始終爲ab.c0000,溼度始終爲de.0。它似乎太「圓潤」成爲一個真正的平均值!一個主意 ? – iero