这是我想在事件上执行的查询,但它返回null,并且不会每周向表中插入任何行。
insert into weekly (winddir,windspeedmph,windgustmph,windgustdir,windspdmph_avg2m,winddir_avg2m,windgustmph_10m,windgustdir_10m,humidity,tempf,rainin,dailyrainin,pressure,batt_lvl,light_lvl)
VALUE (
(select AVG(winddir) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(windspeedmph) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(windgustmph) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(windgustdir) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(windspdmph_avg2m) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(winddir_avg2m) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(windgustmph_10m) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(windgustdir_10m) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(humidity) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(tempf) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(rainin) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(dailyrainin) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(pressure) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(batt_lvl) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()),
(select AVG(light_lvl) from daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW())
)
当我尝试这种方式时:
insert into weekly (winddir,windspeedmph,windgustmph,windgustdir,windspdmph_avg2m,winddir_avg2m,windgustmph_10m,windgustdir_10m,humidity,tempf,rainin,dailyrainin,pressure,batt_lvl,light_lvl)
VALUE (
(select AVG(winddir) from daily ),
(select AVG(windspeedmph) from daily ),
(select AVG(windgustmph) from daily ),
(select AVG(windgustdir) from daily ),
(select AVG(windspdmph_avg2m) from daily ),
(select AVG(winddir_avg2m) from daily ),
(select AVG(windgustmph_10m) from daily ),
(select AVG(windgustdir_10m) from daily ),
(select AVG(humidity) from daily ),
(select AVG(tempf) from daily ),
(select AVG(rainin) from daily ),
(select AVG(dailyrainin) from daily ),
(select AVG(pressure) from daily ),
(select AVG(batt_lvl) from daily ),
(select AVG(light_lvl) from daily )
)
SELECT * FROM daily WHERE `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()
我收到错误1064。
有人可以帮助我。
答案 0 :(得分:1)
首先,您可以通过insert into . . .
使用单个聚合查询来大大简化查询:
Insert into weekly (winddir, windspeedmph, windgustmph, windgustdir, windspdmph_avg2m,
winddir_avg2m, windgustmph_10m, windgustdir_10m, humidity,
tempf, rainin, dailyrainin, pressure, batt_lvl, light_lvl)
select AVG(winddir), AVG(windspeedmph), AVG(windgustmph), AVG(windgustdir), AVG(windspdmph_avg2m),
AVG(winddir_avg2m), AVG(windgustmph_10m), AVG(windgustdir_10m), AVG(humidity),
AVG(tempf), AVG(rainin), AVG(dailyrainin), AVG(pressure), AVG(batt_lvl), AVG(light_lvl)
from daily
where `timeStamp` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) and NOW()
我的猜测是你过去一天没有数据。也许您应该将1 Day
更改为7 Day
(根据您的表名称)。