Jump to content

Archived

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

phpjayx

Using Sum In Mysql

Recommended Posts

$TOTAL5 = "SUM(NUMBER5) from Data where userid='" . $param['userid'];

 

 

Am I doing this incorrectly? I'm not getting the sum of all the NUMBER5 where the userid is as defined....

 

 

Thanks for the help

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
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";

Share this post


Link to post
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 />";

}

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Thanks for the suggestion, that makes sense.... I will try to put it in a separate column instead of comma delimited. Yes I do need to SUM it, but its not the IDs that IM summing its NUMBER5.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.