我有3個非常大的表,每分鐘都有值被記錄下來, 下面是這些表的摘錄。mysql query在1個表中加入3個查詢時的平均值
我想獲得每小時平均爲期這些表的第1天並且相對於投身其中時,請注意,以pH值和溫度
日誌時間之間的幾秒鐘的差距表PH(僅提取,該表是非常大的超過13萬倍的值)
ID time Ph
72176 2013-04-06 03:29:34 7.58
72177 2013-04-06 03:30:34 7.58
72178 2013-04-06 03:31:34 7.54
72179 2013-04-06 03:32:34 7.58
72180 2013-04-06 03:33:34 7.58
72181 2013-04-06 03:34:34 7.58
72182 2013-04-06 03:35:34 7.54
72183 2013-04-06 03:36:34 7.58
72184 2013-04-06 03:37:34 7.54
72185 2013-04-06 03:38:34 7.58
72186 2013-04-06 03:39:34 7.58
表溫度1(僅提取,該表是非常大的超過13萬倍的值)
ID time temperature
133312 2013-04-06 03:29:36 25.37
133313 2013-04-06 03:30:36 25.37
133314 2013-04-06 03:31:36 25.37
133315 2013-04-06 03:32:36 25.31
133316 2013-04-06 03:33:36 25.31
133317 2013-04-06 03:34:36 25.31
133318 2013-04-06 03:35:36 25.37
133319 2013-04-06 03:36:36 25.31
133320 2013-04-06 03:37:36 25.31
133321 2013-04-06 03:38:36 25.31
133322 2013-04-06 03:39:36 25.37
表固體(僅提取物,該表是非常大的超過13萬倍的值)
ID time solids
123791 2013-04-06 03:29:49 140
123792 2013-04-06 03:30:49 140
123793 2013-04-06 03:31:49 143
123794 2013-04-06 03:32:49 140
123795 2013-04-06 03:33:49 140
123796 2013-04-06 03:34:49 140
123797 2013-04-06 03:35:49 140
123798 2013-04-06 03:36:49 143
123799 2013-04-06 03:37:49 140
123800 2013-04-06 03:38:49 140
123801 2013-04-06 03:39:49 140
我目前使用下面的查詢得到小時平均值
SELECT DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00')
, avg(x.solids) avg_solids
FROM solids x where time >= NOW() - INTERVAL 1 DAY
GROUP
BY DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00');
我怎樣纔能有效地加入(相對於時間)上面的查詢結果爲每個傳感器(x3)顯示在1表
=============== ================
低於此查詢獲取的小時值,但不知道如何tweek它得到每小時平均
SELECT DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i') time,
(oT2.temperature) temperature,
(T2.temperature) temp,
(S2.solids) solids,
(P2.Ph) Ph
FROM
(
SELECT minuteTime.minuteTime minuteTime,
(SELECT MAX(time) FROM outside_temperature WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) otempTime,
(SELECT MAX(time) FROM temperature1 WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) tempTime,
(SELECT MAX(time) FROM Ph WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) phTime,
(SELECT MAX(time) FROM solids WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) solidsTime
FROM
(
SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR minuteTime
FROM Ph
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
FROM solids
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
FROM outside_temperature
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
FROM temperature1
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
GROUP BY 1
) minuteTime
) timeTable
LEFT JOIN outside_temperature oT2 ON oT2.time = timeTable.otempTime
LEFT JOIN temperature1 T2 ON T2.time = timeTable.tempTime
LEFT JOIN solids S2 ON S2.time = timeTable.solidsTime
LEFT JOIN Ph P2 ON P2.time = timeTable.phTime
GROUP BY DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i')
ORDER BY minuteTime ASC
沒有必要做這個date_format東西的分組。 'GROUP BY DATE(x.time)'將會完成同樣的事情,而不會強制mysql做一堆反覆的字符串格式化,而這些字符串格式化將會被扔掉。 – 2013-04-05 16:57:23
您是否需要每小時或每天(或兩者)的平均值?或者您是否需要每小時的平均值,然後每小時平均值的平均值? – Barranka 2013-04-05 16:59:04
我只需要每小時的平均值,但只限於顯示1天的平均值 – Ossama 2013-04-05 17:01:16