XCS
XCS

Reputation: 28137

MySQL sum elements of a column

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

Upvotes: 47

Views: 118059

Answers (3)

GolezTrol
GolezTrol

Reputation: 116110

select
  sum(a) as atotal,
  sum(b) as btotal,
  sum(c) as ctotal
from
  yourtable t
where
  t.id in (1, 2, 3)

Upvotes: 13

Ike Walker
Ike Walker

Reputation: 65537

Try this:

select sum(a), sum(b), sum(c)
from your_table

Upvotes: 7

nos
nos

Reputation: 229098

 select sum(A),sum(B),sum(C) from mytable where id in (1,2,3);

Upvotes: 76

Related Questions