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

后端 未结 1 1829
萌比男神i
萌比男神i 2021-01-04 01:43
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

相关标签:
1条回答
  • 2021-01-04 02:02
        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 讨论(0)
提交回复
热议问题