如题,最近学习sql(mysql环境,没有top这种扩展函数,mysql 板块没多少人,特来求救),
遇到一个难题,学生成绩表scores,id,stuid,courid,score
要求查询各科(courid)成绩前两名的stuid,
我自己写了个左连接查询,当成绩没有并列的时候还好,一旦有并列,就不对了,求指教。最好一步完成,不用top函数
select *
from study.scores s1 left join study.scores s2
on s1.courid=s2.courid and s1.score<s2.score
group by s1.courid,s1.score
having count(s1.id)<2
order by s1.courid,s1.score desc