Php or mysql algorithm?

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.