开发者

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

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?开发者_JAVA百科


    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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜