本节课作业:
#6 查询出语文最高分
#8 查询语数外三门成绩的平均分
#9 查询报名孤独求败老师课程的学生信息
表结构:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`Cno` int(11) NOT NULL COMMENT '课程编号',
`Cname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '课程名称',
`Tno` int(11) NULL DEFAULT NULL COMMENT '老师编号',
PRIMARY KEY (`Cno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES (1, '数学', 1);
INSERT INTO `course` VALUES (2, '语文', 2);
INSERT INTO `course` VALUES (3, '英文', 1);
-- ----------------------------
-- Table structure for sc
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
`Sno` int(11) NOT NULL COMMENT '学号',
`Cno` int(11) NOT NULL COMMENT '课程编号',
`score` int(255) NULL DEFAULT NULL COMMENT '分数',
PRIMARY KEY (`Sno`, `Cno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES (1, 1, 99);
INSERT INTO `sc` VALUES (1, 2, 80);
INSERT INTO `sc` VALUES (1, 3, 50);
INSERT INTO `sc` VALUES (2, 1, 70);
INSERT INTO `sc` VALUES (2, 2, 90);
INSERT INTO `sc` VALUES (3, 1, 90);
INSERT INTO `sc` VALUES (4, 1, 60);
INSERT INTO `sc` VALUES (4, 2, 50);
INSERT INTO `sc` VALUES (4, 3, 40);
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`Sno` int(255) NOT NULL COMMENT '学号',
`Sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`Sage` int(255) NULL DEFAULT NULL COMMENT '年龄',
`Ssex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别',
PRIMARY KEY (`Sno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三丰', 108, '男');
INSERT INTO `student` VALUES (2, '李小龙', 20, '男');
INSERT INTO `student` VALUES (3, '小龙女', 17, '女');
INSERT INTO `student` VALUES (4, '白发魔女', 18, '女');
INSERT INTO `student` VALUES (5, '韦小宝', 19, '男');
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`Tno` int(11) NOT NULL COMMENT '老师编号',
`Tname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '老师名称',
PRIMARY KEY (`Tno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (1, '无崖子');
INSERT INTO `teacher` VALUES (2, '孤独求败');
INSERT INTO `teacher` VALUES (3, '洪七公');
SET FOREIGN_KEY_CHECKS = 1;
实战sql:
# 15 - 20
#1 查询年龄小于18岁的学员信息 (5)
select * from student a where a.Sage < 18;
#2 查询无崖子授课信息
select * from teacher d
INNER JOIN course c on d.Tno = c.Tno
where d.TName = '无崖子';
#3 查询没有参与任意课程的学生信息
select * from student a LEFT JOIN sc b on a.Sno = b.Sno
where b.Sno is null;
#4 查询无崖子每个授课课程的学员人数 统计 + 分组
select c.Cno,c.Cname,count(*) from teacher d
INNER JOIN course c on d.Tno = c.Tno
INNER JOIN sc b on b.Cno = c.Cno
where d.TName = '无崖子'
GROUP BY c.Cno,c.Cname;
#5 查询张三丰数学成绩
select * from student a INNER JOIN sc b on a.Sno = b.Sno
INNER JOIN course c on c.Cno = b.Cno
where a.Sname = '张三丰' and c.Cname = '数学';
#6 查询出语文最高分
#7 查询没有参与语文考试的学生信息
# 先查询出参加了语文课程学生
select * from course c
INNER JOIN sc b on c.Cno = b.Cno and c.Cname = '语文'
RIGHT JOIN Student a on a.Sno = b.Sno
where b.Sno is null;
# 和学生做外连接
#8 查询语数外三门成绩的平均分
#9 查询报名孤独求败老师课程的学生信息
#10 没有报名孤独求败老师课程的学生信息
select * from teacher d
INNER JOIN course c on d.Tno = c.Tno and d.Tname = '孤独求败'
INNER JOIN sc b on c.Cno = b.Cno
RIGHT JOIN Student a on a.Sno = b.Sno
where b.Sno is null;
欢迎来到testingpai.com!
注册 关于