MySql sum 一列元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4586040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:11:41  来源:igfitidea点击:

MySql sum elements of a column

mysqlsum

提问by XCS

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...

我有一个包含 3 列(A、B、C)的表格。我想从表中选择一些行,然后 MySQL 返回单行,其中每列都添加了值...

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

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

在这种情况下,如果我选择所有三行,MySql 应该返回:

   A   B  C
1. 12  12 12

回答by nos

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

回答by GolezTrol

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

回答by Ike Walker

Try this:

尝试这个:

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