思路:主要是看每门课程下,每个分数比该课程所有分数小的有几个
保留名次空缺:
select a.cid, a.sid, a.score , count(a.score<b.score)+1 as rank from sc a left join sc b on a.cid=b.cid and a.score<b.score #笛卡尔积连接, 然后筛选满足a.score<b.score的 group by a.cid, a.sid order by a.cid, a.score desc;
不保留名次空缺:
select a.cid, a.sid, a.score , count(distinct b.score)+1 as rank #这里使用的是distinct b.score from sc a left join sc b on a.cid=b.cid and a.score<b.score #笛卡尔积连接, 然后筛选满足a.score<b.score的 group by a.cid, a.sid order by a.cid, a.score desc;
中间表的形式:
select a.cid,a.sid,a.score,b.cid,b.sid,b.score from sc a left join sc b on a.cid=b.cid and a.score< b.score order by a.cid, a.score desc;