兩個表 ordrs, sum_orders, sum_orders 中的count是 orders中相同project_id的count之和;cost同理spa
一條語句直接更新sum_orders中的count & cost3d
#orderscode
#sum_ordersblog
UPDATE sum_orders a INNER JOIN ( SELECT project_id, sum( count ) AS sum_count, sum( cost ) AS sum_cost FROM orders GROUP BY project_id ) b ON a.project_id = b.project_id SET a.count = b.sum_count, a.cost = b.sum_cost;