phpjayx Posted December 29, 2012 Share Posted December 29, 2012 $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 Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/ Share on other sites More sharing options...
requinix Posted December 29, 2012 Share Posted December 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1401963 Share on other sites More sharing options...
phpjayx Posted December 29, 2012 Author Share Posted December 29, 2012 $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. Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1401970 Share on other sites More sharing options...
sowna Posted December 29, 2012 Share Posted December 29, 2012 (edited) 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 December 29, 2012 by sowna Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1401973 Share on other sites More sharing options...
phpjayx Posted December 29, 2012 Author Share Posted December 29, 2012 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 />"; } //////////////// Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1402064 Share on other sites More sharing options...
Barand Posted December 31, 2012 Share Posted December 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1402317 Share on other sites More sharing options...
phpjayx Posted December 31, 2012 Author Share Posted December 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1402412 Share on other sites More sharing options...
Jessica Posted December 31, 2012 Share Posted December 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272474-using-sum-in-mysql/#findComment-1402415 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.