今天在网上查找这个问题的答案,大多数是使用union all进行数据合并,这样写语句很长,灵活性太差,搜索的我很烦,最后无奈自己研究,请看:
表结构如下:
SId是学生id,CId是课程id,score是成绩
查询前五名的语句如下:
select a.SId, a.CId, a.score from score a inner JOIN score b ON a.CId = b.CId AND a.score <= b.score group by a.SId,a.CId,a.score having COUNT( b.SId ) <= 5 order by a.CId,a.score desc
只要一行就可以解决,并且灵活性很高
查询结果如下: