如何根据 sql 中不同科目获得的分数来排序学生排名

how to order student rank on the basis of obtain marks on different subject in sql

这是我们的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

我们希望结果显示为(本质上对成绩求和)

rank    name          total
1       mayank        381
2       ashis         370
3       rohit         314
4       pk            289
5       komal         287

试试这个

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

这将对所有字段求和并按降序对它们进行排序并添加排名。

通过 SELECT @curRank := 0,您可以将所有内容保存在一个 SQL 语句中,而无需先执行 SET。

SET @rank=0;

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

这将产生您想要的结果,如

rank | name | total

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

查看更多详情mysql ranking results