Jump to content

Recommended Posts

Given that single line of code it's quite possible to recover from its problems and continue on correctly... but I doubt that's the case. There's no "SELECT" and there's a missing quote. And no indication the query is actually being run.

 

Post more code.

Link to comment
https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1401963
Share on other sites

$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
https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1401970
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
https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1401973
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
https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1402064
Share on other sites

Don't store the ids as comma delimited lists in a single field. Normalize your data properly and put them in a separate table, one per row with the id of the owning record.

 

Also, are you sure you want to SUM the ids (meaningless) or do you want a COUNT.

Link to comment
https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1402317
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
https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1402415
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.