日期:2014-05-17  浏览次数:20379 次

求SQL!
表A(分数表):
xuehao xingming kemu fenshu
1 小李  语文 60
1 小李  数学 70
1 小李  英语 80
2 小王  语文 80
2 小王  数学 50
2 小王  英语 60


从表A中查出每个人中考得最高分的科目,结果是:
xingming kemu fenshu
小李  英语 80
小王  语文 80

从表A中查出每个科目中考个最高分的人,结果是:
xingming kemu fenshu
小王  语文 80
小李  数学 70
小李  英语 80

很纠结的作业题,希望得到高人讲解!

附加题:
把最高分换成第二高分!

SQL

------解决方案--------------------
xingming kemu fenshu
1:select * from tba as a where fenshu=(select max(fenshu) where tba where a.xingming=xingming)

2:select * from tba as a where fenshu=(select max(fenshu) where tba where a.kemu=kemu)

3:select * from tba as a where fenshu=(select max(fenshu) where tba b where a.xingming=xingming and fenshu<
(select max(fenshu) from tba where b.xingming=xingming)
)