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
查詢結果如下圖: