sql获取每门课程成绩最好的学生信息

时间:2014-05-15 05:02:05   收藏:0   阅读:477

1.相关数据表

Score表

bubuko.com,布布扣

[User]表

bubuko.com,布布扣

SQL语句如下:

--查询出各科成绩最好的学生信息
--自连接
--SELECT TOP 1 * FROM Score B WHERE B.ScoreName = ‘数学‘ ORDER BY B.Score DESC

SELECT A.ID,U.Name,A.ScoreName,A.Score
FROM Score A,[User]U
WHERE UID IN (SELECT TOP 1 UID
                       FROM Score B
                      WHERE B.ScoreName = A.ScoreName
                      ORDER BY B.Score DESC) AND A.UID=U.ID
ORDER BY A.ScoreName, A.Score DESC

 

查询结果如下图:

bubuko.com,布布扣

 

sql获取每门课程成绩最好的学生信息,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!