...

View Full Version : MYSQL: arithmetic operation heeellp!!



trevayne
07-24-2006, 09:53 AM
helo guys nice to be back here,

i got this tiny weenie problem in my DB,

wel im new to mysql too, and i just wnt to know how to add all the numerical values inside a single column. ?

| id | c_code | date | peso |
+----+--------+------------+------+
| 74 | 10 | 1254-07-08 | 564 |
| 75 | 11 | 1235-05-06 | 23 |
| 78 | 4 | 1236-08-07 | 564 |
| 79 | 5 | 1254-07-08 | 564 |
| 82 | 12 | 1235-08-07 | 654 |
| 83 | 13 | 1245-05-06 | 321 |
| 84 | 1 | 1236-08-07 | 654 |

let say i have this table. and i want to add all the values in the peso column..
can anyone share thier expertise on this, i only know the basic operation. i tried learning stored procedures. and im still new to it, please guys i would really appreciate your reply, tnx. :D

As5a5sIn5
07-24-2006, 10:09 AM
Hmm. Not sure if you're using PHP but if you are I think this may help.


//Connected to DB already.
$query1 = mysql_query("SELECT * FROM table_name") //Change table_name!!
while($row = mysql_fetch_assoc($query1)){
$total_peso += $row[peso];
}
echo 'The total of the column "peso" is ' . $total_peso;


Let me know if it works.

GJay
07-24-2006, 11:18 AM
or alternatively
SELECT SUM(columnname) AS total FROM table;

trevayne
07-27-2006, 03:50 AM
hello guys thnks for the answers. yah im using php.

hehe i have a follow up question. how about adding specific id in your table. using sum function. can i do this select sum(peso) as total from <table> where id = this and id = this?
thnks.:D

guelphdad
07-27-2006, 03:52 AM
how about trying it? All it is is a select statement so you aren't going to break anything.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum