Page 1 of 1

Counting values

Posted: Mon Jan 31, 2005 1:32 pm
by syno
if you have a Mysql database stored with dollar values for example
58.20
100.12
15.00
25.35

what is the best to take this information and out put a total for example

58.20
100.12
15.00
25.35
the total is 198.67

again thanks for all your help you have given me!

Posted: Mon Jan 31, 2005 1:43 pm
by timvw
SELECT SUM(*) AS sum FROM values;

Posted: Mon Jan 31, 2005 1:43 pm
by magicrobotmonkey

Code: Select all

SELECT SUM(col_I_want_added) FROM table WHERE something = somethngelse

Posted: Mon Jan 31, 2005 3:50 pm
by thegreatone2176
you could do something like

Code: Select all

$link = "SELECT * FROM payments";
$result = mysql_query($link);
while ($row = mysql_fetch_row($result)){
$price = $rowїmoney];
$total += $price;
}
echo "The total is " . $total;

Posted: Mon Jan 31, 2005 3:52 pm
by John Cartwright
thegreatone2176 wrote:you could do something like

Code: Select all

$link = "SELECT * FROM payments";
$result = mysql_query($link);
while ($row = mysql_fetch_row($result)){
$price = $rowїmoney];
$total += $price;
}
echo "The total is " . $total;
Why would you want to loop through your database when yo ucan get it in one shot

Posted: Mon Jan 31, 2005 4:05 pm
by thegreatone2176
well depending on how else he will format it

he could make a receipt looking form by echoing out each payment in the loop and then after the loop echo the total.

Posted: Tue Feb 01, 2005 1:02 am
by syno

Code: Select all

$sql = mysql_query("SELECT SUM(amount) as total FROM billdata");
while ($row = mysql_fetch_array($sql)) { 
echo $rowї'total']; 
}
give me a value like this 10046766.22

the values im asking it to add for me are like this
100.23
56.43
23.00

etc

any ideas? thanks

Posted: Tue Feb 01, 2005 1:32 am
by timvw
what is the type where you are storing those floating numbers in?

Posted: Tue Feb 01, 2005 9:52 am
by magicrobotmonkey
how many lines are you adding? maybe that's right? You are not using a where clause so it is getting all the records!

Posted: Tue Feb 01, 2005 11:11 pm
by syno
im storing the numbers in a varchar type field in mysql. the reason why is because INT would not store the zero first when i enterd 02 the database only showed 2.

im trying to add every value in a singel col

Posted: Tue Feb 01, 2005 11:28 pm
by feyd
leading zeros can be done during output.. there is the DECIMAL type.