Using Sum In Mysql
#1
Posted 28 December 2012 - 11:55 PM
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
#2
Posted 29 December 2012 - 12:00 AM
Post more code.
#3
Posted 29 December 2012 - 01:10 AM
$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.
#4
Posted 29 December 2012 - 01:49 AM
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, 29 December 2012 - 01:50 AM.
#5
Posted 29 December 2012 - 02:49 PM
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 />";
}
////////////////
#6
Posted 31 December 2012 - 06:34 AM
Also, are you sure you want to SUM the ids (meaningless) or do you want a COUNT.
|
|baaGrid| easy data tables - and more |
#7
Posted 31 December 2012 - 01:40 PM
#8
Posted 31 December 2012 - 01:50 PM
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.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












