这是我的表数据 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应用总和....请告诉我如何实现?
Sum是一个聚合函数。您不需要使用它。这是一个简单的查询-
select *,(maths + chemistry + physics ) AS total FROM `student`
提示:如果其中一个字段可能为 NULL,则使用 COALESCE 将其默认为 0,否则 total
将会为 NULL。
SELECT *, (
COALESCE(maths, 0) +
COALESCE(chemistry, 0) +
COALESCE(physics, 0)
) AS total
FROM `student`
SUM
不是你所需的。SELECT id,
(maths+chemistry+physics) AS total,
maths,
chemistry,
physics
FROM `student`
完全可以胜任。
SUM
进行此操作。尝试使用以下查询:SELECT id, ( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`
+
)。您需要的是以下查询:SELECT id, (`maths` +`chemistry`+`physics`) AS `total`, `maths`, `chemistry`, `physics`
FROM `student`;
试试这个
SELECT id, ( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`
所有的聚合函数都是针对由行名指定的行以及分组操作而工作的。你需要对单独的行执行操作,这对于任何聚合函数来说都不是选项。