Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql sum elements of a column

Tags:

mysql

sum

I have a table with 3 columns (A,B,C). I want to select some rows from the table and then the MySQL to return a single row having the values added on each column.

   A B C 1. 2 2 2 2. 4 4 4 3. 6 7 8 

MySQL should return in this case, if I select all the three rows:

   A   B  C 1. 12  13 14 
like image 337
XCS Avatar asked Jan 03 '11 16:01

XCS


People also ask

How can I get column sum in MySQL using PHP?

like this $sum= mysql_query("SELECT SUM(Value) FROM Codes"); with this i get Resource id #10 but not the sum of all values. Try $result = mysql_query('SELECT SUM(value) AS value_sum FROM codes'); $row = mysql_fetch_assoc($result); $sum = $row['value_sum']; .

How do you sum a column in a query?

You can sum a column of numbers in a query by using a type of function called an aggregate function. Aggregate functions perform a calculation on a column of data and return a single value. Access provides a variety of aggregate functions, including Sum, Count, Avg (for computing averages), Min and Max.

How do you sum a column sum in SQL?

The SQL Server SUM() function is an aggregate function that calculates the sum of all or distinct values in an expression. In this syntax: ALL instructs the SUM() function to return the sum of all values including duplicates. ALL is used by default.


1 Answers

 select sum(A),sum(B),sum(C) from mytable where id in (1,2,3); 
like image 184
nos Avatar answered Sep 25 '22 17:09

nos