Fred708 Posted March 6, 2006 Share Posted March 6, 2006 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! Quote Link to comment Share on other sites More sharing options...
obsidian Posted March 7, 2006 Share Posted March 7, 2006 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! Quote Link to comment Share on other sites More sharing options...
Fred708 Posted March 7, 2006 Author Share Posted March 7, 2006 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, Quote Link to comment Share on other sites More sharing options...
Barand Posted March 7, 2006 Share Posted March 7, 2006 [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] Quote Link to comment Share on other sites More sharing options...
Fred708 Posted March 7, 2006 Author Share Posted March 7, 2006 Thank you very much, got this working perfectly. You folks rock! Quote Link to comment 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.