Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Sum() multiple columns

Tags:

sql

mysql

I have a table of student scorecard. here is the table,

subject  | mark1 | mark2 | mark3 |......|markn stud1    | 99    | 87    | 92    |      | 46 stud2    |....................................   .   . studn    |....................................| 

Now, i need to sum it for each student of total marks. I got it by using sum(mark1+mark2+...+markn) group by stud. I want to know how to sum it without adding each column name,it will be huge when in case up to marks26. so could anyone know how to fix it. Thanks in advance.

like image 915
rk3265423 Avatar asked Mar 13 '14 04:03

rk3265423


People also ask

How do I sum multiple columns in MySQL?

SELECT CONCAT('SELECT ', group_concat(`COLUMN_NAME` SEPARATOR '+'), ' FROM scorecard') FROM `INFORMATION_SCHEMA`. `COLUMNS` WHERE `TABLE_SCHEMA` = (select database()) AND `TABLE_NAME` = 'scorecard' AND `COLUMN_NAME` LIKE 'mark%'; The query above will generate another query that will do the selecting for you.

How do I sum all columns in SQL?

The SQL AGGREGATE SUM() function returns the SUM of all selected column. Applies to all values. Return the SUM of unique values. Expression made up of a single constant, variable, scalar function, or column name.

How can I sum two rows in MySQL?

SUM() function. MySQL SUM() function returns the sum of an expression. SUM() function returns NULL when the return set has no rows.


Video Answer


1 Answers

SELECT student, (SUM(mark1)+SUM(mark2)+SUM(mark3)....+SUM(markn)) AS Total  FROM your_table  GROUP BY student 
like image 120
Maulik patel Avatar answered Oct 04 '22 10:10

Maulik patel