MySQL求一列元素的和

47

我有一个包含3个列(A、B、C)的表格。我想要从中选择一些行,然后让MySQL返回一行,在该行中每列的值都是相加后的结果。

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

如果我选择全部三行,MySQL 应该返回什么结果:

   A   B  C
1. 12  13 14
3个回答

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

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

7

试试这个:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接