使用SUM函数更新表时,GROUP函数的使用无效

2022-02-24 00:00:00 sql-update sql mysql

我有两个表:o_daily_lcsgenerationo_daily_generation

尝试更新o_daily_generation时收到错误消息:

error(1111) invalid use of Group function

以下是我正在运行的代码:

update o_daily_generation join o_daily_lcsgeneration 
on o_daily_generation.Location =o_daily_lcsgeneration.Location 
   and o_daily_generation.Date =o_daily_lcsgeneration.Date  
set o_daily_lcsgeneration.Turbine_Generation =sum(o_daily_generation.Turbine_Generation)

解决方案

改为尝试此操作:

UPDATE o_daily_generation od
INNER JOIN 
(
     SELECT Location, SUM(Turbine_Generation) TurbineSum
     FROM o_daily_lcsgeneration
     GROUP BY Location
) og ON od.Location = og.Location
SET od.Turbine_Generation = og.TurbineSum

相关文章