Cómo agregar valores de columna en mysql


Estos son los datos de mi tabla Student

introduzca la descripción de la imagen aquí

Y esta es mi pregunta {

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

Pero está lanzando una sola fila {

id  total   maths   chemistry   physics
118     760     55  67  55

Aunque quiero aplicar suma para todos los id ....déjame saber cómo puedo lograr esto?

Author: Trialcoder, 2012-09-12

6 answers

Sum es una función agregada. No necesitas usarlo. Esta es la consulta simple -

select *,(maths + chemistry + physics ) AS total FROM `student`
 61
Author: Piyu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-12 11:34:17

Si está requiriendo obtener calificaciones totales de cada estudiante, entonces SUM no es lo que necesitaría.

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

Hará el trabajo muy bien.

 10
Author: hjpotter92,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-12 11:31:42

No necesita usar SUM para esta operación. Prueba esta consulta:

SELECT id, ( maths + chemistry + physics ) AS total, maths, chemistry, physics
FROM `student`
 8
Author: khomyakoshka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-12 11:32:42

Consejo: Si uno de los campos tiene la posibilidad de ser NULL, entonces use COALESCE para predeterminarlos a 0, de lo contrario total resultará en NULL.

SELECT *, (
    COALESCE(maths, 0) 
    + COALESCE(chemistry, 0) 
    + COALESCE(physics, 0)
) AS total 
FROM `student`
 1
Author: digout,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-25 12:47:37

Toda la función aggregate funciona en filas especificadas por rowname y group by operation. Necesita operación en filas individuales que no es una opción para ninguna función agregada.

 0
Author: Saikat Biswas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-23 10:50:20

Prueba esto

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

Has terminado. Gracias

 0
Author: Y. Joy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-21 08:19:49