Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of Multiple rows in MySql

Tags:

sql

mysql

sum

I have data in this manner

ID       SUB       Marks    
 1      English      25
 1       Maths       22
 1      Science      15
 2      English      16
 2       Maths       20
 2      Science      12

And so on...

How would I sum all subject marks and give total marks of each ID?

For example, if I select ID=1 then it should show 62.

like image 583
Aryan Manoj Raj Avatar asked Sep 11 '12 18:09

Aryan Manoj Raj


Video Answer


3 Answers

This should do it:

SELECT ID, SUM(Marks) as totalMarks
FROM MyTable
GROUP BY Id
like image 160
Abe Miessler Avatar answered Oct 19 '22 19:10

Abe Miessler


select id, sum(Marks) as marks
from tablename
group by id
like image 30
Gratzy Avatar answered Oct 19 '22 21:10

Gratzy


Try this... this is working...

SELECT ID, SUM(Marks) as Total
FROM Table_Name
GROUP BY ID
ORDER BY ID
like image 34
Ajs Rock Avatar answered Oct 19 '22 19:10

Ajs Rock