MYSQL Trigger Update column with same column based on other column -
i have following table.
order_id code value 1 sub_total 5.00 1 shipping 5.00 1 total 10.00 2 sub_total 3.00 2 shipping 6.00 2 total 9.00
on occasion sub_total need update (products added instance), , total needs updated. have created trigger update sub_total. how update total based on sub_total , shipping?
i have attempted following:
create trigger after_order_product_update after update on order_product each row begin update `order_total` set `value` = new.`total` `order_id` = new.`order_id` , `code` = 'sub_total'; update `order_total` set `value` = (select `value` `order_total` `code` = 'shipping' , `order_id` = new.`order_id`) + (select `value` `order_total` `code` = 'sub_total' , `order_id` = new.`order_id`) `code` = 'total' , `order_id` = new.`order_id`; end;
here receive "you can't specify target table 'oc_order_total' update in clause error no: 1093"
create trigger after_order_product_update after update on order_product each row begin declare sbvalue varchar(32); declare shipvalue varchar(32); update `order_total` set `value` = new.`total` `order_id` = new.`order_id` , `code` = 'sub_total'; set sbvalue = (select `value` `order_total` `code` = 'sub_total' , `order_id` = new.`order_id`); set shipvalue = (select `value` `order_total` `code` = 'shipping' , `order_id` = new.`order_id`); update `oc_order_total` set `value` = `sbvalue` + `shipvalue` `code` = 'total' , `order_id` = new.`order_id`; end;
here receive "subquery returns more 1 row" error.
set sbvalue = (select `value` `order_total` `code` = 'sub_total' , `order_id` = new.`order_id` limit 1); set shipvalue = (select `value` `order_total` `code` = 'shipping' , `order_id` = new.`order_id` limit 1);
give limit 1 second query error..
hope helps
Comments
Post a Comment