mysql - Error : <table_name> specified twice, both as a target for 'UPDATE' and as a separate source for data -
i want update tot_sales attribute tot_sales sum of order_price's of orders handled salesman
i wrote update query. gives error
#1093 - table 'salesman' specified twice, both target 'update' , >as separate source data
update salesman set salesman.tot_sales = (select sum(orders.order_price) orders inner join salesman on orders.emp_id = salesman.emp_id group salesman.emp_id )
using approach, use correlated subquery rather aggregation:
update salesman s set s.tot_sales = (select sum(o.order_price) orders o o.emp_id = s.emp_id );
this written as:
update salesman s left join (select o.emp_id, sum(o.order_price) total_price orders o group o.emp_id ) o on o.emp_id = s.emp_id set s.tot_sales = o.total_price;
Comments
Post a Comment