Jump to content

[SOLVED] Ineed help on MySQL/PHP calculation


Recommended Posts

Hi guys.

 

I need to achieve a certain result through PHP and MySQL maths functions.

 

I have a tableasfollows

 

ItemID, ProductName, Quantity, Value

 

I can get the total quantity.

 

I need to get the total value. This requires me to get the total sum of the product of quantity and Value

 

something like SUM(Quantity*Value)

 

if I have 3 records as follows:

 

ItemID ProductName Quantity Value

1                Fruits        3        100

2                Books        5        200

3                Machines    2        500

 

The total value of fruits is 300, books 1000 and machines 1000. Total value of all is 2300.

 

How do I achieve this (2300) in SQL and PHP?

 

ie 3 * 100

  5 * 200

  2 * 500

 

 

I hope I'm clear

 

Hi gnawz,

 

I wonder if something like this may work:

 

$valuequery = "SELECT ProductName, SUM(value) FROM TABLE_NAME GROUP BY ProductName";
$quantityquery = "SELECT ProductName, SUM(quantity) FROM TABLE_NAME GROUP BY ProductName";

if ( ($valueresult = mysql_query($valuequery)) && ($quantityresult = mysql_query($quantityquery)) ) { 

     while (($row=mysql_fetch_assoc($valueresult))||($row=mysql_fetch_assoc($quantityresult))) { 

        $result = $row['SUM(value)'] * $row['SUM(quantity)'];
echo $result;

      } 
   } 

 

I'm not sure if the above would work, in theory it should get the total quantity of each Product, then get the quantity of each price for that product, multiple it and return the result.

 

I may be way off with this so apologies in advance if I am but give it a go and see what results you get.

Hi jcombs_31, that query would effectively do 10 x 8 wouldn't it? 

 

I might be wrong as that statement is what I was going to suggest at first but thought it would give the wrong number gnawz is looking for.

 

No, that query will give exactly the result the OP is looking for.  Don't know where you came up with 10*8. There is no need for multiple queries or php to do the math.

That's because a query returns a result resource and you need to use one of the mysql_fetch_xxxx statements to get a row from the result set - http://us3.php.net/manual/en/function.mysql-fetch-assoc.php

Im using mysql_fetch_array() and my query is;

 

$sql = "SELECT SUM(Quantity *Value) FROM stocktable AS TOTAL";

$result = dbQuery($sql);

 

while($row = mysql_fetch_array($result))

{

$totalvalue = $row["SUM(Quantity*Value)"];

}

 

Also,I have noticed one may omit the phrase "as total" from the sql.

 

Besides that,what could be the issue?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.