Php or mysql algorithm? This is the result of the database query.
Now you need to display the result as (the price was originally a straight result, so that I can easily write the formula ):
Price order_id
(20/60) * 700x0045183
(40/60) * 700x0045183
Zero x 0045178
The calculation idea is: the same order number, (20/60) * 70, (40/60) * 70, different order numbers directly take the price value.
Reply to discussion (solution)
Is 60 specified? There are still 70, and the above result is * 700, so I can't understand it.
Is 60 specified? There are still 70, and the above result is * 700, so I can't understand it.
The calculation idea is: the same order number, (20/60) * 700, (40/60) * 700, different order numbers directly take the price value. Yes. 700 is obtained by adding the same order price. 60 is the sum of cost3 in the same order.
Optimized SQL
select cost3, (cost3/sum_cost3)*sum_price as price, a.order_id form tbl_name a, (select order_id, sum(cost3) as sum_cost3, sum(price) as sum_price from tbl_name group by order_id) t where a.order_id=t.order_id
Php is still troublesome.
After direct query
while($r = mysql_fetch_assoc($rs)) { $st[$r['order_id']['cost3'] += $r['cost3']; $st[$r['order_id']['price'] += $r['price'];}mysql_data_seek($rs, 0);while($r = mysql_fetch_assoc($rs)) { $res[] = ($r['cost3']/$st['cost3'])*$st['price'];}
select a.order_id , (select (a.cost3/sum(cost3) * sum(price)) from od where order_id=a.order_id group by order_id) as price from od a;
The above methods are all practical. thank you.
select a.order_id , (select (a.cost3/sum(cost3) * sum(price)),sum(price) from od where order_id=a.order_id group by order_id) as price from od a;
But why do I add a sum (price) prompt: Operand shoshould contain 1 column (s)
SQL code? 123 select. order_id, (select (. cost3/sum (cost3) * sum (price), sum (price) from od where order_id =. order_id group by order_id) as price from od a; but why is a sum (price) added? tip: Operand ...... Why not add another field?
There are two fields in the subquery, which cannot be written.
select a.order_id , (select (a.cost3/sum(cost3) * sum(price)) from od where order_id=a.order_id group by order_id) as price,(select sum(price) from od where order_id=a.order_id group by order_id) as sum_price from od a;