使用平均分数更新MYSQL

时间:2013-11-18 11:35:42

标签: mysql

我有两个表流体和cas,我​​试图用cad表的平均分数更新相关的表液。像这样:

UPDATE fluids INNER JOIN cas on cas.cas = fluids.cas 
SET fluids.score = avg(cas.score) GROUP BY cas.cas

1 个答案:

答案 0 :(得分:1)

您需要将聚合选择包装为子查询:

UPDATE fluids 
INNER JOIN (
    SELECT cas,avg(cas.score) as avg_score
    FROM cas
    GROUP BY cas 
) as c ON c.cas = fluids.cas 
SET fluids.score = c.avg_score