9, July 2008

find the sum of column values - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  Databases  >  MySQL
Topic: find the sum of column values
« previous next »
Pages: [1] Print

Author Topic: find the sum of column values  (Read 674 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« on: Sep 08, 2006, 07:27:11 PM »

say i have this table, fusion_hiscores. one of the field values for each row in this table is "player_cmb" i want to find all the values for player_cmb, say 1 row is 110 and the next row is 20, and another one is 120. that is the only 3 in the table, so i want a function that finds all these values no matter how many rows exist, and then add them all together. how would i go about doing this? this is what i am trying:

Code:
<?php

$input 
dbresult(dbquery("SELECT player_cmb FROM ".$db_prefix."hiscores"), 0);

function 
cmb_average($input) {
return 
array_sum($input) / count($input);
}
echo 
cmb_average();
?>

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #1 on: Sep 08, 2006, 07:42:13 PM »

here is revised code that is returning 120 as the cmb_average, when 120 and 110 are the only 2 values in teh database, so the avg should be 115 :S

Code:
<?php
$input 
dbarray(dbquery("SELECT player_cmb FROM ".$db_prefix."hiscores"));
function 
cmb_total($input) {
return 
array_sum($input);
}
function 
cmb_average($input) {
return 
array_sum($input) / count($input);
}
echo 
cmb_average($input);
echo 
cmb_total($input);
?>

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Sep 08, 2006, 11:28:30 PM »

try the function AVG() from the mysql manual


Last blog : 4th of July Lottery from TemplateMonster.com
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #3 on: Sep 09, 2006, 01:08:32 AM »

when i do that it gives me the call to undefined function error... how would i define this as a new function?

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #4 on: Sep 09, 2006, 02:12:42 AM »



Last blog : Are You Stumbling Yet?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #5 on: Sep 09, 2006, 02:36:39 AM »

this is what i got but it doesnt show anything :S

Code:
<?php
$cmb_avg 
dbquery("SELECT count(player_id) as player_id, SUM(player_cmb) as player_cmb, (SUM(player_cmb)/count(player_id)) as combat_avg FROM ".$db_prefix."hiscores");
$combat_average dbarray($cmb_avg);
echo 
$combat_average;
?>

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #6 on: Sep 09, 2006, 04:42:36 AM »

i have found the solution.. if anyone wants solution for finding average of particular columns of data let me know

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7943
40607 credits
Members referred : 3



« Reply #7 on: Sep 09, 2006, 11:24:23 AM »

If you want the average, then the solution is:

SELECT AVG(field) FROM table

If you want the summary then :

SELECT SUM(field) FROM table

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : MIA - Where Nick and Tim
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=4010
Tags : mysql databases Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  Databases  >  MySQL
Topic: find the sum of column values
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Jul 09, 2008, 12:05:30 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 35.587
Total Topics: 7.355
Total Members: 3.661
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: komrad

24 Guests, 3 Users online :

18 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.