如何添加在mysql中列值(How to add column values in mysql)

2019-06-28 06:04发布

这是我的表中的数据Student

这是我的查询 -

SELECT id, SUM( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`

但抛单排 -

id  total   maths   chemistry   physics
118     760     55  67  55

虽然我想申请总和所有的ID ....让我知道我能做到这一点?

Answer 1:

点心是一个聚合函数。 你不需要使用它。 这是一个简单的查询 -

select *,(maths + chemistry + physics ) AS total FROM `student`


Answer 2:

如果你需要得到每个学生的总标志,然后SUM是不是你需要什么。

SELECT id,
    (maths+chemistry+physics) AS total,
    maths,
    chemistry,
    physics
FROM `student`

将做的工作就好了。



Answer 3:

你不需要使用SUM进行此项操作。 试试这个查询:

SELECT id, ( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`


Answer 4:

提示:如果其中一个字段必须是空的可能性,然后使用COALESCE默认这些0,否则total将导致NULL。

SELECT *, (
    COALESCE(maths, 0) +
    COALESCE(chemistry, 0) +
    COALESCE(physics, 0)
) AS total 
FROM `student`


Answer 5:

所有聚合函数可以工作在rowname和group by操作指定行。 你需要对各行的操作是不是任何聚合函数的一个选项。



Answer 6:

试试这个

SELECT id, ( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`

你完成了。 谢谢



Answer 7:

在MySQL的SUM函数在某种意义上说,它给你值的一列从你的SELECT语句的总和作品。 如果您需要从行查询值求和,就用加号( + ),你需要的是这个查询:

SELECT id, (`maths` +`chemistry`+`physics`) AS `total`, `maths`, `chemistry`, `physics`
FROM `student`;

这会给你你需要的结果。



文章来源: How to add column values in mysql