gnawz Posted September 22, 2009 Share Posted September 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/ Share on other sites More sharing options...
Bricktop Posted September 22, 2009 Share Posted September 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-922922 Share on other sites More sharing options...
jcombs_31 Posted September 22, 2009 Share Posted September 22, 2009 SELECT SUM(quantity * value) AS total from table; Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-922926 Share on other sites More sharing options...
Bricktop Posted September 22, 2009 Share Posted September 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-922928 Share on other sites More sharing options...
jcombs_31 Posted September 22, 2009 Share Posted September 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923055 Share on other sites More sharing options...
gnawz Posted September 22, 2009 Author Share Posted September 22, 2009 Thanks guys. However,what I need is to get the total or sum of quantity * value not sum of quantity * sum of value. ie (quantity * value + quantity * value + quantity * value),etc like in my example table above. Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923126 Share on other sites More sharing options...
PFMaBiSmAd Posted September 22, 2009 Share Posted September 22, 2009 The query that jcombs_31 posted gives 2300 as the total. That is what you stated the result should be. Did you even try it? Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923136 Share on other sites More sharing options...
gnawz Posted September 22, 2009 Author Share Posted September 22, 2009 Hi? It looks like it should be working but I get "Resource id#8" when I "echo $result;" Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923140 Share on other sites More sharing options...
PFMaBiSmAd Posted September 22, 2009 Share Posted September 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923144 Share on other sites More sharing options...
gnawz Posted September 22, 2009 Author Share Posted September 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923162 Share on other sites More sharing options...
mikesta707 Posted September 22, 2009 Share Posted September 22, 2009 he made it as total so you could refer to it as $row['total']; so change it to $totalvalue = $row['total']; Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923165 Share on other sites More sharing options...
gnawz Posted September 23, 2009 Author Share Posted September 23, 2009 Actually it worked! Just sth missing in SQL. I will try row as total as well. Good for me to learn,thanks Quote Link to comment https://forums.phpfreaks.com/topic/175109-solved-ineed-help-on-mysqlphp-calculation/#findComment-923201 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.