0

Here is our table

name         math     physics     chemistry    hindi    english
pk           85       65            45          54       40
ashis        87       44            87          78       74
rohit        77       47            68          63       59
mayank       91       81            78          47       84
komal        47       51            73          61       55

we want to result show as (summing the grades essentially)

rank    name          total
1       mayank        381
2       ashis         370
3       rohit         314
4       pk            289
5       komal         287
Henry
  • 564
  • 3
  • 22
Pk Prajapati
  • 11
  • 1
  • 6
  • Possible duplicate of [How to SUM two fields within an SQL query](https://stackoverflow.com/questions/14877797/how-to-sum-two-fields-within-an-sql-query) – Henry Jun 21 '17 at 17:57
  • Please show your code to get a quality answer. – TomServo Jun 21 '17 at 17:59

2 Answers2

0

Try this

SELECT @curRank := @curRank + 1 AS rank, name, (math + physics + chemistry + hindi + history) AS total FROM table, (SELECT @curRank := 0) r ORDER BY total DESC;

This will sum all the fields and sort them by descending order and add a rank.

By doing SELECT @curRank := 0 you can keep it all in one SQL statement without having to do a SET first.

Henry
  • 564
  • 3
  • 22
0
SET @rank=0;

SELECT @rank:=@rank+1 AS rank,name,(math+physics+chemistry+hindi+english) as total
FROM tablename ORDER BY total DESC

this will produce your desired result as

rank | name | total

--------------------
1    | mayank | 381
2    | ashis  | 370

for more details take a look mysql ranking results

vijay
  • 493
  • 5
  • 19