Mysql在同一个表中对多个科目成绩的不同学生进行排名

bqjvbblv  于 12个月前  发布在  Mysql
关注(0)|答案(2)|浏览(123)

我想根据科目对学生进行排名,以及他们对该特定科目的总体排名。查询适用于所有科目,但当我指定where子句时,我只获得where子句中指定的用户的排名,并且排名始终为1,似乎没有检查其他ID


的数据

SELECT 
    Student_id,
    Subject,
    Total_Overal,
    RANK() OVER (PARTITION BY Subject ORDER BY Total_Overal DESC) as StudentRank
FROM
    exams 
WHERE 
    Student_id = "155676" 
    AND Subject = "French";

字符串

but5z9lq

but5z9lq1#

因为where子句只产生一行结果,所以排名总是1。你需要在周围添加另一层SELECT:

SELECT * FROM
(SELECT Student_id,
    Subject,
    Total_Overal,
    RANK() OVER (PARTITION BY Subject ORDER BY Total_Overal DESC) as StudentRank
FROM exams) t
WHERE Student_id="155676" and Subject="French";

字符串

qij5mzcb

qij5mzcb2#

如果你理解SQL的执行顺序,你就会明白为什么你会得到你所得到的。WHERESELECT之前被计算。

FROM and JOIN.
WHERE.
GROUP BY.
ROLLUP , CUBE , GROUPING SETS.
HAVING.
OVER (e.g., Window Functions)
SELECT.
DISTINCT.

字符串
https://vladmihalcea.com/sql-operation-order/

相关问题