-- 附:本书中常用的数据库teaching中的表结构和表记录。 -- 创建学生信息表student表 create table if not exists student ( studentno char(11) not null comment'学号', sname char(8) not null comment'姓名', sex enum('男', '女') default '男' comment'性别', birthdate date not null comment'出生日期', entrance int(3) null comment'入学成绩', phone varchar(12) not null comment'电话', Email varchar(20) not null comment'电子信箱', primary key (studentno) ); -- 插入数据到学生信息表student表 insert into student values ('20112100072','许东方','男','2002/2/4',658,'12545678998','[email protected]' ) , ('20112111208','韩吟秋','女','2002/2/14',666,'15878945612','[email protected]'), ('20120203567','封白玫','女','2003/9/9', 898,'13245674564','[email protected]'), ('20120210009','崔舟帆','男','2002/11/5',789,'13623456778','[email protected]'), ('20123567897','赵雨思','女','2003/8/4', 879,'13175689345','[email protected]'), ('20125121109','梁一苇','女','2002/9/3', 777,'13145678921','[email protected]'), ('20126113307','姚扶嵋','女','2003/9/7', 787,'13245678543','[email protected]'), ('21125111109','敬秉辰','男','2004/3/1', 789,'15678945623','[email protected]'), ('21125221327','何桐影','女','2004/12/4',879,'13178978999','[email protected]'), ('21131133071','崔依歌','男','2002/6/6', 787,'15556845645','[email protected]'), ('21135222201','夏文斐','女','2005/10/6',867,'15978945645','[email protected]'), ('21137221508','赵临江','男','2005/2/13',789,'12367823453','[email protected]'); -- 创建课程信息表course create table if not exists course ( courseno char(6) not null, cname char(6) not null, type char(8) not null, period int(2) not null, exp int(2) not null, term int(2) not null, primary key (courseno) ); -- 插入数据到课程信息表course INSERT into course values ('c05103','电子技术','必修','64','16','2'), ('c05109','C语言','必修','48','16','2'), ('c05127','数据结构','必修','64','16','2'), ('c05138','软件工程','选修','48','8','5'), ('c06108','机械制图','必修','60','8','2'), ('c06127','机械设计','必修','64','8','3'), ('c06172','铸造工艺','选修','42', '16','6'), ('c08106','经济法','必修','48','0','7'), ('c08123','金融学','必修','40','0','5'), ('c08171','会计软件','选修','32','8','8'); -- 创建学生分数表score。 create table if not exists score ( studentno char(11) not null, courseno char(6) not null, daily float(4,1) default 0, final float(4,1) default 0, primary key (studentno , courseno) ); -- 插入数据到分数表score INSERT INTO score (studentno, courseno, daily, final) VALUES ('20112100072', 'c05103', 99, 92), ('20120203567', 'c05103', 78, 67), ('20120210009', 'c05103', 65, 98), ('20125121109', 'c05103', 88, 79), ('21125111109', 'c05103', 96, 97), ('21137221508', 'c05103', 77, 92), ('20112100072', 'c05109', 95, 82), ('20120203567', 'c05109', 87, 86), ('20125121109', 'c05109', 77, 82), ('20126113307', 'c05109', 89, 95), ('21125111109', 'c05109', 87, 82), ('21125221327', 'c05109', 89, 95), ('20120210009', 'c05138', 88, 89), ('21137221508', 'c05138', 74, 91), ('20112111208', 'c06108', 77, 82), ('20120210009', 'c06108', 79, 88), ('20123567897', 'c06108', 99, 99), ('20126113307', 'c06108', 78, 67), ('20112111208', 'c06127', 85, 91), ('20120203567', 'c06127', 97, 97), ('20112111208', 'c06172', 89, 95), ('21125221327', 'c06172', 88, 62), ('21131133071', 'c06172', 78, 95), ('21125111109', 'c08106', 77, 91), ('21135222201', 'c08106', 91, 77), ('21137221508', 'c08106', 89, 62), ('21131133071', 'c08123', 78, 89), ('21135222201', 'c08123', 79, 99), ('20112100072', 'c08171', 82, 69), ('20125121109', 'c08171', 85, 91), ('21131133071', 'c08171', 88, 98), ('21135222201', 'c08171', 85, 92); -- 创建教师信息表teacher create table if not exists teacher ( teacherno char(6) not null comment '教师编号', tname char(8) not null comment'教师姓名', major char(10) not null comment '专业', prof char(10) not null comment '职称', department char(16) not null comment '部门', primary key (teacherno) ); -- 插入数据到教师信息表teacher insert into teacher values ('t05001', '苏超然', '软件工程', '教授', '计算机学院'), ('t05002', '常可观', '会计学', '助教', '管理学院'), ('t05003', '孙释安', '网络安全', '教授', '计算机学院'), ('t05011', '卢敖治', '软件工程', '副教授','计算机学院'), ('t05017', '茅佳峰', '软件测试', '讲师', '计算机学院'), ('t06011', '夏期年', '机械制造', '教授', '机械学院'), ('t06023', '卢释舟', '铸造工艺', '副教授','机械学院'), ('t07019', '韩庭宇', '经济管理', '讲师', '管理学院'), ('t08017', '白成园', '金融管理', '副教授','管理学院'), ('t08058', '孙有存', '数据科学', '副教授','计算机学院'); -- 创建纽带表teach_course。 create table if not exists teach_course ( teacherno char(6) not null, courseno char(6) not null, primary key (teacherno,courseno) ); -- 插入数据到纽带表teach_course INSERT INTO teach_course (teacherno, courseno) VALUES ('t05001', 'c05103'), ('t05002', 'c05109'), ('t05003', 'c05127'), ('t05011', 'c05138'), ('t05017', 'c06108'), ('t05017', 'c06172'), ('t06011', 'c06127'), ('t06023', 'c05127'), ('t06023', 'c06172'), ('t07019', 'c08106'), ('t08017', 'c08123'), ('t08058', 'c08171'); -- 创建选课信息表se_course create table se_course (sc_no int(6) not null auto_increment, studentno char(11) not null, courseno char(6) not null, teacherno char(6) not null, score int(3) null, sc_time timestamp not null default now(), primary key (sc_no) ); -- 插入数据到选课信息表se_course INSERT INTO se_course (sc_no, studentno, courseno, teacherno, score, sc_time) VALUES (1, '21125111109', 'c06172', 't05017', NULL, '2020-12-09 18:33:45'), (2, '20120210009', 'c06108', 't06023', NULL, '2020-12-24 18:30:15'), (3, '20123567897', 't01239', 't05003', NULL, '2020-12-26 18:09:09'); mysql 查询姓名为 “韩吟秋” 的学生的学号studentno、选修的课程名称cname和期末成绩final
时间: 2025-07-11 22:02:01 浏览: 10
### 查询实现
为了完成这一查询任务,需要从三个表中提取数据:`student` 表用于查找学生姓名对应的学生编号 (`studentno`),`score` 表用于关联学生的课程成绩,以及 `course` 表用于获取具体的课程名称 (`cname`)。以下是完整的 SQL 查询语句及其解释。
#### SQL 查询
```sql
SELECT
s.studentno AS '学号',
c.cname AS '课程名称',
sc.final AS '期末成绩'
FROM
student s
JOIN
score sc ON s.studentno = sc.studentno
JOIN
course c ON sc.courseno = c.courseno
WHERE
s.sname = '韩吟秋';
```
#### 解析
1. **表连接**
- 使用 `JOIN` 将 `student` 表与 `score` 表基于 `studentno` 进行连接[^1]。
- 再次使用 `JOIN` 将 `score` 表与 `course` 表基于 `courseno` 进行连接[^3]。
2. **筛选条件**
- 在 `WHERE` 子句中指定 `s.sname = '韩吟秋'`,从而仅返回名为“韩吟秋”的学生的记录[^1]。
3. **字段选择**
- 选取 `studentno`(学号)、`cname`(课程名称)和 `final`(期末成绩)作为最终输出的结果集[^1]。
---
### 注意事项
- 如果存在同名学生,则此查询可能返回多个结果。在这种情况下,可以根据额外的信息(如班级或其他唯一标识符)进一步过滤。
- 若某位学生尚未参加任何考试或其成绩为空值,在默认设置下这些记录不会显示于结果集中。如果希望包含这样的情况,可考虑改用左外连接 (`LEFT JOIN`) 替代标准内连接 (`INNER JOIN`)。
---
### 示例扩展
假如还需要统计每位符合条件的学生所选修的所有课程总数及平均分数,可以在原基础上增加聚合计算部分:
```sql
SELECT
s.studentno AS '学号',
s.sname AS '姓名',
COUNT(sc.courseno) AS '选课数量',
AVG(sc.final) AS '平均成绩'
FROM
student s
JOIN
score sc ON s.studentno = sc.studentno
JOIN
course c ON sc.courseno = c.courseno
WHERE
s.sname = '韩吟秋'
GROUP BY
s.studentno, s.sname;
```
此处新增了两个聚合函数——`COUNT()` 记录选课数目,`AVG()` 计算平均分,并通过 `GROUP BY` 对每个学生单独汇总[^1]。
---
阅读全文
相关推荐


















