SQL查询成绩表中的每科第三名

1 篇文章 0 订阅

SQL查询成绩表中的每科第三名

 题目要求

这里写图片描述

 表格数据

  • grade表
c_idc_noc_name
1180
1290
1370
1495
2164
2288
2389
2479
3190
3280
3396
3485
4188
4290
4350
4460

  • course表
c_idc_noc_name
1001C/C++
2002JAVA
3003Linux
4004数据库

  1. student表
s_ids_nos_name
1001gb
2002lly
3003pdl
4004lb

解题过程

  • 第一步 先根据成绩表得出每科每名学生的排名
查询代码
SELECT
    t1.*, (
        SELECT
            count(*)
        FROM
            grade t2
        WHERE
            t1.score <= t2.score
        AND t1.c_id = t2.c_id
    ) AS rownum
FROM
    grade t1
查询结果
c_ids_idscorerownum
11803
12902
13704
14951
21644
22882
23891
24793
31902
32804
33961
34853
41882
42901
43504

  • 第二步 将第一步所的表命名为t3 然后将表中rownum为3的查询出来 并按学科排序
查询代码
SELECT
    *
FROM
    (
        SELECT
            t1.*, (
                SELECT
                    count(*)
                FROM
                    grade t2
                WHERE
                    t1.score <= t2.score
                AND t1.c_id = t2.c_id
            ) AS rownum
        FROM
            grade t1
    ) t3
WHERE
    rownum = 3
ORDER BY
    c_id
查询结果
c_ids_idscorerownum
11803
24793
34853
44603

  • 第三步 对最终查询结果与 student cource 两表进行左连接
查询代码
SELECT
    student.s_no,
    student.s_name,
    course.c_name,
    grade_temp.score
FROM
    (
        SELECT
            *
        FROM
            (
                SELECT
                    t1.*, (
                        SELECT
                            count(*)
                        FROM
                            grade t2
                        WHERE
                            t1.score <= t2.score
                        AND t1.c_id = t2.c_id
                    ) AS rownum
                FROM
                    grade t1
            ) t3
        WHERE
            rownum = 3
        ORDER BY
            c_id,
            score DESC
    ) grade_temp
LEFT JOIN course ON grade_temp.c_id = course.c_id
LEFT JOIN student ON grade_temp.s_id = student.s_id
查询结果
s_ids_namec_namescore
001gbC/C++80
004lbJAVA79
004lbLinux85
004lb数据库60

附上表结构和表数据sql


SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `c_id` int(11) NOT NULL,
  `c_no` varchar(20) NOT NULL,
  `c_name` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('1', '001', 'C/C++');
INSERT INTO `course` VALUES ('2', '002', 'JAVA');
INSERT INTO `course` VALUES ('3', '003', 'Linux');
INSERT INTO `course` VALUES ('4', '004', '数据库');

-- ----------------------------
-- Table structure for grade
-- ----------------------------
DROP TABLE IF EXISTS `grade`;
CREATE TABLE `grade` (
  `c_id` int(11) NOT NULL,
  `s_id` int(11) NOT NULL,
  `score` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of grade
-- ----------------------------
INSERT INTO `grade` VALUES ('1', '1', '80');
INSERT INTO `grade` VALUES ('1', '2', '90');
INSERT INTO `grade` VALUES ('1', '3', '70');
INSERT INTO `grade` VALUES ('1', '4', '95');
INSERT INTO `grade` VALUES ('2', '1', '64');
INSERT INTO `grade` VALUES ('2', '2', '88');
INSERT INTO `grade` VALUES ('2', '3', '89');
INSERT INTO `grade` VALUES ('2', '4', '79');
INSERT INTO `grade` VALUES ('3', '1', '90');
INSERT INTO `grade` VALUES ('3', '2', '80');
INSERT INTO `grade` VALUES ('3', '3', '96');
INSERT INTO `grade` VALUES ('3', '4', '85');
INSERT INTO `grade` VALUES ('4', '1', '88');
INSERT INTO `grade` VALUES ('4', '2', '90');
INSERT INTO `grade` VALUES ('4', '3', '50');
INSERT INTO `grade` VALUES ('4', '4', '60');

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `s_id` int(11) NOT NULL,
  `s_no` varchar(20) NOT NULL,
  `s_name` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '001', 'gb');
INSERT INTO `student` VALUES ('2', '002', 'lly');
INSERT INTO `student` VALUES ('3', '003', 'pdl');
INSERT INTO `student` VALUES ('4', '004', 'lb');
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值