我想将一个表中的平均月值插入到另一个表中作为平均值,但它不能很好地运行
`UPDATE avg_month_val
set
year = YEAR(STR_TO_DATE(`date`, '%Y-%m-%d')),
month = MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
evep = ROUND(AVG(`evep`), 2),
sunshine_hrs = ROUND(AVG(`sunshine_hrs`) ,2),
rainfall = ROUND(SUM(`rainfall`), 2),
max_temp = ROUND(AVG(`max_temp`), 2),
min_temp = ROUND(AVG(`min_temp`), 2)
FROM
reg_data3
GROUP BY
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')) `
答案 0 :(得分:0)
不知道这是发布时的复制错误,但是
GROUP BY
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d'))
没有意义,应该显示错误。你应该做
GROUP BY
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
YEAR(STR_TO_DATE(`date`, '%Y-%m-%d'))
同样update
只更改现有行中的值,并且在大多数情况下需要where
- 子句来分隔要更改的行。用于插入您想要的值
INSERT INTO avg_month_val (`year`,`month`,`evep`,`sunshine_hrs`,`rainfall`,`max_temp`,`min_temp`)
select
YEAR(STR_TO_DATE(`date`, '%Y-%m-%d')),
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
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
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d'))