How to update fields that is the aggregate result of another table in MySQL?

只谈情不闲聊 提交于 2019-11-30 01:01:42

问题


UPDATE  a JOIN  b ON a.app_id=b.app_id GROUP BY a.app_id SET 

remark_avg=AVG(b.score),remark_count=COUNT(b.id);

The above is basically what I want to do,but it's not a valid MySQL statement,how to write it correctly?


回答1:


    UPDATE a
INNER JOIN (SELECT AVG(b.score) avg_score,
                   COUNT(b.id) cnt_id,
                   b.app_id
              FROM b
          GROUP BY b.app_id) x ON x.app_id = a.app_id
       SET remark_avg = x.avg_score,
           remark_count = x.cnt_id;


来源:https://stackoverflow.com/questions/5752075/how-to-update-fields-that-is-the-aggregate-result-of-another-table-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!