oracle查询各个年级的总分数,1、查询各班各科分数最高的学生学号,姓名,班级名称,科目名称,分数:...

博客内容讲述了如何通过SQL查询从数据库中获取每个班级每门课程的最高分学生信息。原始查询方法存在效率问题,通过使用子查询和UNION ALL进行多次查询,虽然能获取正确结果,但效率低下。文章提出了优化需求,旨在减少查询复杂性和提高执行效率。
摘要由CSDN通过智能技术生成

3、分数表 score:

±---------±------±-------±------+

| courseID | stuID | course | score |

±---------±------±-------±------+

| 2 | 1001 | 数学 | 73 |

| 3 | 1001 | 英语 | 79 |

| 1 | 1001 | 语文 | 81 |

| 3 | 1002 | 英语 | 87 |

| 2 | 1002 | 数学 | 83 |

| 1 | 1002 | 语文 | 79 |

| 1 | 1003 | 语文 | 65 |

| 3 | 1003 | 英语 | 65 |

| 2 | 1003 | 数学 | 97 |

| 1 | 1004 | 语文 | 78 |

| 3 | 1004 | 英语 | 78 |

| 2 | 1004 | 数学 | 86 |

| 1 | 1005 | 语文 | 67 |

| 3 | 1005 | 英语 | 88 |

| 2 | 1005 | 数学 | 89 |

| 2 | 1006 | 数学 | 90 |

| 3 | 1006 | 英语 | 92 |

| 1 | 1006 | 语文 | 98 |

| 1 | 1007 | 语文 | 85 |

| 2 | 1007 | 数学 | 78 |

| 3 | 1007 | 英语 | 72 |

| 1 | 1008 | 语文 | 78 |

| 3 | 1008 | 英语 | 77 |

| 2 | 1008 | 数学 | 85 |

| 3 | 1009 | 英语 | 94 |

| 2 | 1009 | 数学 | 91 |

| 1 | 1009 | 语文 | 68 |

±---------±------±-------±------+

1、查询各班各科分数最高的学生学号,姓名,班级名称,科目名称,分数:

MySQL>

create table ab_all_cl_st_sc as select st.stuid as 学号 , st.stuname as 姓名 , l.classname as 班级, sc.course as 科目, sc.score as 成绩 from ab_class cl , ab_student st , ab_score sc where cl.classid=st.classid and st.stuid=sc.stuid

MySQL>

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘一班’ and tt.科目=‘语文’)

union all

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘一班’ and tt.科目=‘数学’)

union all

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘一班’ and tt.科目=‘英语’)

union all

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘二班’ and tt.科目=‘语文’)

union all

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘二班’ and tt.科目=‘数学’)

union all

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘二班’ and tt.科目=‘英语’)

union all

select * from AB_ALL_CL_ST_SC t where t.科目=‘语文’ and t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘三班’ and tt.科目=‘语文’)

union all

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘三班’ and tt.科目=‘数学’)

union all

select * from AB_ALL_CL_ST_SC t where t.成绩=(

select max(tt.成绩) from AB_ALL_CL_ST_SC tt where tt.班级=‘三班’ and tt.科目=‘英语’)

结果:

538ebe6916bf8cc41ccb0054b986bc1a.png

如下代码效果不佳

MySQL>select max(成绩),班级,科目 from AB_ALL_CL_ST_SC t group by 班级,科目

结果

4915d7b48bb7164713f17ad77a880bfa.png

font face=“微软雅黑” color=#FF8C00 size=3> 修改颜色

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值