sql查询出各科成绩最好的学生信息
程序员文章站
2023-12-01 22:57:16
1.相关数据表 score表 [user]表 sql语句如下: 复制代码 代码如下: --查询出各科成绩最好的学生信息 --自连接 --select top 1...
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
查询结果如下图:
下一篇: 46 个非常有用的 PHP 代码片段