How to update final associated record
Check it here: http://rextester.com/MGKS16637

I'm getting max(Id) and difference between sum(fee) and total price from items table. If this value is &lt;> 0, then update the item with id=max(id).

update items
       join (select max(i.id) mid, o.totalPrice - sum(i.fee) as diff
           from items i
           inner join orders o on i.order_Id = o.orderId
           group by i.order_id
           order by i.order_id, i.id) v
       on items.id = v.mid
set items.fee = items.fee + v.diff
where v.diff &lt;&gt; 0;