如何在mysql中增加一个值?我想找到max tot必须为1的学生成绩,依此类推
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在mysql中增加一个值?我想找到max tot必须为1的学生成绩,依此类推相关的知识,希望对你有一定的参考价值。
我希望在每行之后递增一个值@a值必须递增
SET @a=1;
SELECT @a,std_id,SUM(`read_mark`+`write_mark`+`homework`+`classwork`) AS tot FROM tbl_exam
where `class_detail_id`=26 AND YEAR(`date`)=1397 GROUP BY `std_id` ORDER BY
SUM(`read_mark`+`write_mark`+`homework`+`classwork`) DESC;
SET @a=@a+1;
答案
你可以尝试下面
SET @a = 0;
select (@a:=@a + 1) AS num,* from
(SELECT
std_id,SUM(`read_mark`+`write_mark`+`homework`+`classwork`) AS tot
FROM tbl_exam
where `class_detail_id`=26 AND YEAR(`date`)=1397 GROUP BY `std_id`
ORDER BY tot DESC
)X
对于mysql Version 8+,您可以使用窗口函数row_number()
select row_number() over(partition by std_id order by tot desc) as num,*
from
(SELECT
std_id,SUM(`read_mark`+`write_mark`+`homework`+`classwork`) AS tot
FROM tbl_exam
where `class_detail_id`=26 AND YEAR(`date`)=1397 GROUP BY `std_id`
ORDER BY tot DESC
)X
以上是关于如何在mysql中增加一个值?我想找到max tot必须为1的学生成绩,依此类推的主要内容,如果未能解决你的问题,请参考以下文章
为什么`if(id = max(id),id,id + 1)在mysql中无法正常工作