2014-09-24 36 views
0

這是我想對事件執行的查詢,但它返回null,並且它不會每週向表中插入任何行。關於事件的MySQL錯誤1064

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

有人可以幫我請。

回答

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(根據你的表名)。