我的桌子:
hourly_weather electrical_readings
---------------- -----------------------
meter | time_read | temp meter | time | kwh
---------------- -----------------------
1 1316044800 55 1 1316136250 19.24
1 1316138400 56 1 1316044320 18.29
(...) (...)
我想从这些数据中检索两个重要的值:
1)我想要给定一天的总KW
2)我想要那天的最高温度
我正在使用的查询需要 WAYYYY 太长时间才能运行,但我想不出另一种方法来做到这一点。就像,两个表中的 100,000 行数据需要几个小时。
SELECT * FROM (
SELECT * , SUM(kwh) AS sumkwh,
DATE( FROM_UNIXTIME( r.time_read ) ) AS datex,
UNIX_TIMESTAMP( DATE( FROM_UNIXTIME( r.time_read ) ) ) AS datey,
(
SELECT MAX( temp )
FROM hourly_weather hw
WHERE hw.meter = 1
AND time_read >= datey
AND time_read < datey + 86400
) AS temp
FROM electrical_readings r
WHERE id = 1
GROUP BY datex
) as t1
WHERE t1.temp != '';