Jump to content


Photo

Using Sum In Mysql


  • Please log in to reply
7 replies to this topic

#1 phpjayx

phpjayx

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts

Posted 28 December 2012 - 11:55 PM

$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

#2 requinix

requinix

    Playful Member

  • Moderators
  • 5,744 posts
  • LocationWA

Posted 29 December 2012 - 12:00 AM

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.

#3 phpjayx

phpjayx

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts

Posted 29 December 2012 - 01:10 AM

$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.

#4 sowna

sowna

    Member

  • Members
  • PipPip
  • 29 posts

Posted 29 December 2012 - 01:49 AM

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, 29 December 2012 - 01:50 AM.


#5 phpjayx

phpjayx

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts

Posted 29 December 2012 - 02:49 PM

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 />";
}
////////////////

#6 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 13,885 posts
  • LocationCheshire, UK

Posted 31 December 2012 - 06:34 AM

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.

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#7 phpjayx

phpjayx

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts

Posted 31 December 2012 - 01:40 PM

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.

#8 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

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.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

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

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com