Jump to content


Newbie Question, Working With Data

  • Please log in to reply
4 replies to this topic

#1 Fred708

  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 06 March 2006 - 11:56 PM


This is probably a very easy question to answer, I honestly have no clue. I can work with the very basics of php and slight editing if the code is already there, but I have no clue really on how to go about this.

What I'm trying to do is add all the values stored in a particular column (about 840 values atm and growing), and hopefully divide that number by the total amount. (averaging it). I can do the displaying and such on my own, I have no idea how you would actually get all those numbers to add and such.

Any idea on how one could do this? Thanks for any help!

#2 obsidian

  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 March 2006 - 12:08 AM

welcome to the forums! hope you find lots of good help here.

to get averages, sums, and counts, you need to run aggregate functions in your SQL query. don't worry, it sounds a lot harder than it is! ;-)... if i want to find the sum of all the values in one column, i simply can select the SUM() of that column. for instance:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] SUM(points) AS points FROM games; [!--sql2--][/div][!--sql3--]

that sql statement would return only one record containing a column called "points" that holds the value of all the records added together. same thing with average:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] AVG(points) AS points FROM games; [!--sql2--][/div][!--sql3--]

hope that helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Fred708

  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 07 March 2006 - 04:37 AM

Thanks for the quick reply, I appreciate it!

I'm sure that's what I want to be doing (fairly sure), but I can't seem to get it right.

The column I'm looking to average is item_value, which is in eqdkp_items, which is in the database eqdkp.

So how correct is this?

$query="SELECT AVG(points) AS points FROM eqdkp_items";

I guess I'm having trouble going from eqdkp_items to item_value, which is the one I'd like to average.

Sorry, I'm clueless. Thanks again for the quick reply,

#4 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 07 March 2006 - 01:03 PM

$query="SELECT AVG([color=#FF6666]item_value[/color]) AS average FROM eqdkp_items";

$res = mysql_query($query);

$av_value = mysql_result ($res, 0, 'average');

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Fred708

  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 07 March 2006 - 10:34 PM

Thank you very much, got this working perfectly. You folks rock!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users