Let's say i ha this table:
ID | Bytes | 1 | 21 | 2 | 8 | 3 | 4 |
My question is how to sum (21+8+4) in field "Bytes" so the output will be 32. I don't about "sum" function in mysql or php.
Thanks for the answer
SELECT SUM(Bytes) FROM table_name;
Easy, isn't it? 🙂
Hi, You can use the query $qry="select sum(Bytes) bytes_sum from $tablename"; Here bytes_sum will become the field name i.e you have to use $row["bytes_sum"] inorder to get the sum(i am assuming that you are using while($row=mysql_fetch_array($res)). Note that you can't select any other data if you use that query. If you need to select the other data also then i think the only option you are left with is sum the values in while loop. ex: while($row=mysql_fetch_array($res)) { $bytes_sum=$bytes_sum+$row["Bytes"]; }
Hope this helps.