查询每科成绩前3和第3的学生

1、sql文件:
DROP TABLE IF EXISTS `studentscore`;
CREATE TABLE `studentscore` (
  `scoreid` int(11) NOT NULL AUTO_INCREMENT,
  `sourceid` int(11) DEFAULT NULL,
  `userid` int(11) DEFAULT NULL,
  `score` int(11) DEFAULT NULL,
  PRIMARY KEY (`scoreid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

INSERT INTO `studentscore` VALUES ('1', '1', '1', '60');
INSERT INTO `studentscore` VALUES ('2', '2', '1', '80');
INSERT INTO `studentscore` VALUES ('3', '3', '1', '95');
INSERT INTO `studentscore` VALUES ('4', '1', '2', '75');
INSERT INTO `studentscore` VALUES ('5', '2', '2', '85');
INSERT INTO `studentscore` VALUES ('6', '3', '2', '90');
INSERT INTO `studentscore` VALUES ('7', '4', '1', '89');
INSERT INTO `studentscore` VALUES ('8', '4', '2', '88');
INSERT INTO `studentscore` VALUES ('9', '1', '3', '78');
INSERT INTO `studentscore` VALUES ('10', '2', '3', '87');
INSERT INTO `studentscore` VALUES ('11', '3', '3', '98');
INSERT INTO `studentscore` VALUES ('12', '4', '3', '76');
INSERT INTO `studentscore` VALUES ('13', '1', '4', '67');
INSERT INTO `studentscore` VALUES ('14', '2', '4', '76');
INSERT INTO `studentscore` VALUES ('15', '3', '4', '88');
INSERT INTO `studentscore` VALUES ('16', '4', '4', '77');

结果:
在这里插入图片描述

2、查询每科成绩第3的学生

sql语句:

select * from studentscore a 
where (select count(*)+1 from studentscore b where a.sourceid = b.sourceid and b.score > a.score) = 3;

结果:
在这里插入图片描述
解释:

1、假设a.sourceid=1,a.score=78(这是课程1中第1高的分数):

select count(*) from studentscore b where 1 = b.sourceid and b.score > 78

如果a.score是课程1中第1高的分数,那表b中课程1中的score比78大的没有1个,所以count(*)是0,返回值就是0

select count(*)+1 from studentscore b where 1 = b.sourceid and b.score > 78

如果我们把count(*)后面加上1,结合上面的分析,可以知道上面sql语句的返回值是1,所以如果a.score是课程1中第1高的分数,那么返回值就是1

2、假设a.sourceid=1,a.score=75(这是课程1中第2高的分数):

select count(*) from studentscore b where 1 = b.sourceid and b.score > 75

如果a.score是课程1中第2高分数,那表b中课程1中的score比75大的只有1个,所以count(*)是1,返回值就是1

select count(*)+1 from studentscore b where 1 = b.sourceid and b.score > 75

如果我们把count(*)后面加上1,结合上面的分析,可以知道上面sql语句的返回值是2,所以如果a.score是课程1中第2高的分数,那么返回值就是2

以此类推可知:

如果采用count(*)+1这种写法,a.score是课程1中第几高的分数,返回值就是几,所以我们第3高的分数返回值等于3

拓展:

通过上面的分析,查询每科成绩第3的学生也可以这样做:

select * from studentscore a 
where (select count(*) from studentscore b where a.sourceid = b.sourceid and b.score >= a.score) = 3;
3、查询每科成绩前3的学生

sql语句:

select * from studentscore a 
where (select count(*)+1 from studentscore b where a.sourceid = b.sourceid and b.score > a.score) <= 3 
order by sourceid,score desc;

结果:
在这里插入图片描述
解释:

可以上面的解释知道,如果采用count(*)+1这种写法,a.score是课程1中第几高的分数,返回值就是几,所以a.score如果是课程1中前3高的分数,那返回值应该小于等于3,然后order by sourceid,score desc代表先按照sourceid排序,在按照score排序

拓展:

通过上面的分析,查询每科成绩前3的学生也可以这样做:

select * from studentscore a 
where (select count(*) from studentscore b where a.sourceid = b.sourceid and b.score >= a.score) <= 3 
order by sourceid,score desc;
4、参考文章:

1、https://blog.csdn.net/sandyagor/article/details/79664086
2、https://blog.csdn.net/qq_31871785/article/details/74946411?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-5

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
西华大学成绩查询V2.1是通过选课课号查询成绩详单的工具。西华大学所有同学可以使用本软件提查询教师已提交成绩。 使用准备: 1、请确保电脑安装了Microsoft Excel 2003或者更高版本; 2、打开软件之,请关闭所有Excel表,以及Microsoft Excel程序; 3、在查询成绩详单时,请不要自行打开Excel表。 使用方法: 1.双击西华大学成绩查询.exe运行程序; 2.使用自己的帐号登录教务系统,点击“信息查询”菜单,选择“学生选课情况查询”,找到自己要查询课程的选课课号,如:(2012-2013-2)-4200049-0120030086-2。补考成绩将最后一位数改成“补1”,重修成绩将最后一位数改为“重1”或“重2”之类的; 3.将在第2步得到对的选课号复制到软件的选课号编辑框中,单击“查询”按钮,稍等片刻。 注意事项: 1.第一次使用软件时,软件会在运行目录中自动生成一个文本文件(选课号.txt),用于记录选课号,请勿随意删除,否则将会丢失已经记录的选课号; 2.软件底部的“记住”按钮用于记录组合框中现行的选课号,“清空”按钮将会清空所有选课号记录; 3.选课号.txt文件中有选课号范本,文件的每一行为一条选课号记录,手动增加记录后加上回车符,重新运行软件即可; 4.对于没有出来的成绩查询到的结果只有课程的基本信息,没有具体数据; 5.查询成绩单,需要把该课程的所有学生成绩下载完毕之后才能显示出来,所以请耐心等待; 6.外网用户查询速度较慢,建议使用校园内网查询; 7.若需复制成绩单,在成绩详单显示出来之后,单击开始菜单,程序,Microsoft Office,Microsoft Office Excel 2003(或者其他更高版本Excel),将软件界面中你需要的成绩复制下来,然后到Excel表粘贴。或者单击成绩详单中的任一单元格,然后再次单击“查询”按钮,会有弹窗信息框,“该文档已经修改,是否保存修改结果?”,选择是,保存类型选择Excel工作簿。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值