Jump to content

Archived

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

Fred708

Newbie Question, Working With Data

Recommended Posts

Hey!

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!

Share this post


Link to post
Share on other sites
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] [color=blue]SUM[/color](points) [color=green]AS[/color] points [color=green]FROM[/color] [color=orange]games;[/color] [!--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] [color=blue]AVG[/color](points) [color=green]AS[/color] points [color=green]FROM[/color] [color=orange]games;[/color] [!--sql2--][/div][!--sql3--]

hope that helps!

Share this post


Link to post
Share on other sites
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?

[code]$query="SELECT AVG(points) AS points FROM eqdkp_items";[/code] ?

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,

Share this post


Link to post
Share on other sites
[code]$query="SELECT AVG([color=#FF6666]item_value[/color]) AS average FROM eqdkp_items";

$res = mysql_query($query);

$av_value = mysql_result ($res, 0, 'average');[/code]

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.