将平均月份值插入到另一个表中,但是当前月平均值应该预先传递到表

时间:2013-11-26 12:23:43

标签: mysql sql

此查询效果很好。我想阻止这个月的平均值传递给avg_month_val1我该怎么做。

INSERT IGNORE INTO `clima_data`.`avg_month_val1`  ( `year` , `month` ,  
                                `evep` , `sunshine_hrs` , `rainfall` , 
                                `max_temp` , `min_temp` ) 
SELECT year(str_to_date(date, '%m/%d/%Y'))as year, 
       month(str_to_date(date, '%m/%d/%Y'))as month,
       round(avg(evep),2),
       round(Avg(sunshine_hrs),2),
       round(sum(rainfall),2),
       round(AVG(max_temp),2),
       round(avg(min_temp),2) 
FROM reg_data3
GROUP BY year(str_to_date(date, '%m/%d/%Y')),
         month(str_to_date(date, '%m/%d/%Y')) 
ORDER BY 1 Desc;

1 个答案:

答案 0 :(得分:0)

也许这符合您的意图:

INSERT IGNORE INTO `clima_data`.`avg_month_val1`  ( `year` , `month` ,  
                                `evep` , `sunshine_hrs` , `rainfall` , 
                                `max_temp` , `min_temp` ) 
SELECT year(str_to_date(date, '%m/%d/%Y'))as year, 
       month(str_to_date(date, '%m/%d/%Y'))as month,
       round(avg(evep),2),
       round(Avg(sunshine_hrs),2),
       round(sum(rainfall),2),
       round(AVG(max_temp),2),
       round(avg(min_temp),2) 
FROM reg_data3
GROUP BY year(str_to_date(date, '%m/%d/%Y')),
         month(str_to_date(date, '%m/%d/%Y')) 
HAVING (year(str_to_date(date, '%m/%d/%Y')) <> year(CURRENT_TIMESTAMP)
     OR month(str_to_date(date, '%m/%d/%Y')) <> month(CURRENT_TIMESTAMP) )
ORDER BY 1 Desc;