sql查询出各科成绩最好的学生信息
时间:2021-07-10 09:33:42|栏目:MsSql|点击: 次
1.相关数据表
Score表
[User]表
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
查询结果如下图:
Score表
[User]表
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
查询结果如下图:
栏 目:MsSql
下一篇:SQL小技巧 又快又简单的得到你的数据库每个表的记录数
本文标题:sql查询出各科成绩最好的学生信息
本文地址:http://www.codeinn.net/misctech/155449.html