Jump to content

Math on mysql


gokihar

Recommended Posts

Hi, I'm new here !

I have some problems and hope you can help me a bit :

Got table with :

Product | quantity | price | (quantity*price) AS value

It all works fine but I cannot get the sum of value from all of the products.

 

In first part of the code I got select and table draw. After closing table I try to sum value :

$query2="SELECT sum(quantity*price) as value2 FROM table";
if($query2){echo " Command q2 is successful ";}
else {echo " Command q2 is not successful ";} 
$result2=mysql_query($query2);
if($result2){echo " Command r2 is successful ";}
else {echo " Command r2 is not successful ";} 
$zmienna=mysql_fetch_array($query2);
if($zmienna){echo " Command z is successful ";}
else {echo " Command z is not successful ";} 

echo " Result: ".$query2['value2'];

 

query2 give me "S" as output and I just don't know why. Any hints ?

Link to comment
https://forums.phpfreaks.com/topic/237508-math-on-mysql/
Share on other sites

Actually, looks like you can:

 

http://dev.mysql.com/doc/refman/5.0/en/select.html

 

The first comment there might help you:

 

  Quote
SELECT

SUM(IF(beta_idx=1, beta_value,0)) as beta1_value,

SUM(IF(beta_idx=2, beta_value,0)) as beta2_value,

SUM(IF(beta_idx=3, beta_value,0)) as beta3_value

FROM alpha JOIN beta WHERE alpha_id = beta_alpha_id;

 

Link to comment
https://forums.phpfreaks.com/topic/237508-math-on-mysql/#findComment-1220499
Share on other sites

FYI, in your first code, which I don't really get, you would change the last bit to (comments in the code):

$zmienna=mysql_fetch_array($result2); //need to fetch the result2, not query2
if($zmienna){echo " Command z is successful ";}
else {echo " Command z is not successful ";} 

echo " Result: ".$zmienna['value2'];  //need to use zmienna, because it holds the result set (array data), not query2 which is just a string

Link to comment
https://forums.phpfreaks.com/topic/237508-math-on-mysql/#findComment-1220681
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.