sql - MySQL Update existing value with Inner join -


i updating summary table detail table using inner join follows

drop temporary table if exists summ ; drop temporary table if exists det  ;  create temporary table summ (id int , val int ) ; create temporary table det (id int , val int ) ;  insert summ(id,val) value (1,0) ; insert summ(id,val) value (2,0) ;  insert det(id,val) value (1,10) ; insert det(id,val) value (1,10) ; insert det(id,val) value (1,20) ;  update summ inner join det on summ.id = det.id  set summ.val = summ.val+ det.val  ;  select * summ  id = 1; 

its showing value of val 10 instead of 40 .. wrong ? using mysql 5.1 on windows

try joining table against subquery calculates total val every id.

update  summ          inner join          (             select  id, sum(val) totalval                det             group   id         ) det on summ.id = det.id  set     summ.val = det.totalval 

Comments