13, February 2012

find the sum of column values - webmaster forum

 
Webdigity webmaster forums
[ 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
Instabuck - The easy way to sell digital products online

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



« on: Sep 08, 2006, 06: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();
?>


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



« Reply #1 on: Sep 08, 2006, 06: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);
?>


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


It's time to use PHP5!


« Reply #2 on: Sep 08, 2006, 10:28:30 pm »

try the function AVG() from the mysql manual


Last blog : A new Wordpress theme for our blog
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #3 on: Sep 09, 2006, 12: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?


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, 01:12:42 am »



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



« Reply #5 on: Sep 09, 2006, 01: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;
?>


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



« Reply #6 on: Sep 09, 2006, 03:42:36 am »

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


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



« Reply #7 on: Sep 09, 2006, 10: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
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
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....

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?
Feb 13, 2012, 05:55:46 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!






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