Jump to content

Using Sum In Mysql


phpjayx

Recommended Posts

$sql_select = "SELECT *, SUM(NUMBER5) from Data where userid='" . $param['userid'] . "' order by timestamp asc";

 

$query = mysql_query($sql_select, $link);

 

$result = mysql_fetch_array($query);

return $result;

 

 

This is the other code I was trying to play with... again, I haven't gotten it to work. So I'm sure there are lots of things wrong with it.... I'm a newbie with this stuff.

Link to comment
Share on other sites

I think combining * and aggregate function in select query together will not give right answer...

 

You can use like this, to get sum of number5,

 

$sql_select = "SELECT SUM(NUMBER5) from Data where userid='" . $param['userid'] . "' order by timestamp asc";

 

or if you want to find sum of all users, you can use like this...

 

$sql_select = "SELECT userid, SUM(NUMBER5) from Data group by userid";

Edited by sowna
Link to comment
Share on other sites

Ok, thanks for the help that helped me get this below to work.... but now I need to add onto this... If I have an unkown number of userid's I want to add up, which will be stored in one field (OWNEROF), this will be comma dilleniated (example 14, 23, 49).... So still adding up NUMBER5, but where userid= any of those values... How do I get that into the below query?

 

Along the same lines... is there a way in the database itself to assign a field to always SUM values?

 

/////////////////////////////////////////////////////////////////////////

$query = "SELECT *, SUM(NUMBER5) FROM Data where userid='" . $param['userid'] . "' order by timestamp asc";

 

$result = mysql_query($query) or die(mysql_error());

 

// Print out result

while($row = mysql_fetch_array($result)){

echo "Total ". $row['type']. " = ^". $row['SUM(NUMBER5)'];

echo "<br />";

}

////////////////

Link to comment
Share on other sites

If I have an unkown number of userid's I want to add up, which will be stored in one field (OWNEROF), this will be comma dilleniated (example 14, 23, 49).... So still adding up NUMBER5, but where userid= any of those values... How do I get that into the below query?

 

you don't. You NEED to normalize your data.

Link to comment
Share on other sites

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.