gpt4 book ai didi

mysql - 如何在mysql上按年份和学期排名?

转载 作者:行者123 更新时间:2023-11-29 20:10:46 25 4
gpt4 key购买 nike

我的数据库(MySQL)表中有数据

SELECT * FROM `student_result_group`

结果

enter image description here

我试试:

SELECT year, term, grade_id, student_id, total_exam1, total_exam2, total,
FIND_IN_SET( total, ( SELECT GROUP_CONCAT( total ORDER BY total DESC ) FROM student_result_group)) AS rank
from student_result_group
GROUP BY year, term, grade_id, student_id

我得到这样的结果:

enter image description here

我想要这个结果:

enter image description here

我应该编写哪个查询来获取每个学期和每年的排名?

感谢您的帮助。

最佳答案

只是这个查询:

    select
s1.year,
s1.term,
s1.student_id,
s1.grade_id,
s1.total_exam1,
s1.total_exam2,
s1.total,
count(s2.total)+1 AS rank
from
student_result_group s1 LEFT JOIN student_result_group s2
ON s1.grade_id = s2.grade_id AND s1.student_id != s2.student_id AND s1.term = s2.term AND s1.year = s2.year AND s1.total <= s2.total
GROUP by
s1.student_id,
s1.year,
s1.term,
s1.grade_id
ORDER BY
s1.year, s1.term, s1.total, s1.student_id

关于mysql - 如何在mysql上按年份和学期排名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40144808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com