MySQL 如何在mysql中添加列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12387061/
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
How to add column values in mysql
提问by Trialcoder
This is my table data Student
这是我的表数据 Student
And this is my query --
这是我的查询——
SELECT id, SUM( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`
but it is throwing a single row --
但它抛出了一行——
id total maths chemistry physics
118 760 55 67 55
although i want to apply sum for all ids ....let me know how can i achieve this?
虽然我想为所有 id 申请 sum ....让我知道我怎样才能做到这一点?
回答by Piyu
Sum is a aggregate function. You dont need to use it. This is the simple query -
Sum 是一个聚合函数。你不需要使用它。这是简单的查询 -
select *,(maths + chemistry + physics ) AS total FROM `student`
回答by hjpotter92
If you're requiring to get total marks of each student, then SUM
is not what you'd be needing.
如果您需要获得每个学生的总分,那么SUM
这不是您所需要的。
SELECT id,
(maths+chemistry+physics) AS total,
maths,
chemistry,
physics
FROM `student`
Will do the job just fine.
会很好地完成工作。
回答by khomyakoshka
You don't need use SUM
for this operation. Try this query:
您不需要使用SUM
此操作。试试这个查询:
SELECT id, ( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`
回答by digout
回答by Benson Kiprono
The sum function in MySQL works in the sense that it gives you the sum of values in a column from your select statement. If you need to sum values from a row in your query, then just use plus (+
) What you need is this query :
MySQL 中的 sum 函数的工作原理是它为您提供 select 语句中列中值的总和。如果您需要对查询中一行的值求和,那么只需使用 plus ( +
) 您需要的是这个查询:
SELECT id, (`maths` +`chemistry`+`physics`) AS `total`, `maths`, `chemistry`, `physics`
FROM `student`;
This will give you the results you need.
这将为您提供所需的结果。
回答by Y. Joy Ch. Singha
Try this
尝试这个
SELECT id, ( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`
You are done. Thanks
你完成了。谢谢
回答by Saikat Biswas
All aggregate function works on rows specified by rowname and group by operation. You need operation on individual rows which is not an option for any aggregate function.
所有聚合函数都作用于由 rowname 和 group by 操作指定的行。您需要对单个行进行操作,这不是任何聚合函数的选项。