mysql续集5-多表查询练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql续集5-多表查询练习相关的知识,希望对你有一定的参考价值。
init.sql
/*
数据导入:
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : mysql
Source Server Version : 50624
Source Host : localhost
Source Database : sqlexam
Target Server Type : MySQL
Target Server Version : 50624
File Encoding : utf-8
Date: 10/21/2016 06:46:46 AM
*/
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES (‘1‘, ‘三年二班‘), (‘2‘, ‘三年三班‘), (‘3‘, ‘一年二班‘), (‘4‘, ‘二年九班‘);
COMMIT;
-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES (‘1‘, ‘生物‘, ‘1‘), (‘2‘, ‘物理‘, ‘2‘), (‘3‘, ‘体育‘, ‘3‘), (‘4‘, ‘美术‘, ‘2‘);
COMMIT;
-- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`num` int(11) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_score_student` (`student_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES (‘1‘, ‘1‘, ‘1‘, ‘10‘), (‘2‘, ‘1‘, ‘2‘, ‘9‘), (‘5‘, ‘1‘, ‘4‘, ‘66‘), (‘6‘, ‘2‘, ‘1‘, ‘8‘), (‘8‘, ‘2‘, ‘3‘, ‘68‘), (‘9‘, ‘2‘, ‘4‘, ‘99‘), (‘10‘, ‘3‘, ‘1‘, ‘77‘), (‘11‘, ‘3‘, ‘2‘, ‘66‘), (‘12‘, ‘3‘, ‘3‘, ‘87‘), (‘13‘, ‘3‘, ‘4‘, ‘99‘), (‘14‘, ‘4‘, ‘1‘, ‘79‘), (‘15‘, ‘4‘, ‘2‘, ‘11‘), (‘16‘, ‘4‘, ‘3‘, ‘67‘), (‘17‘, ‘4‘, ‘4‘, ‘100‘), (‘18‘, ‘5‘, ‘1‘, ‘79‘), (‘19‘, ‘5‘, ‘2‘, ‘11‘), (‘20‘, ‘5‘, ‘3‘, ‘67‘), (‘21‘, ‘5‘, ‘4‘, ‘100‘), (‘22‘, ‘6‘, ‘1‘, ‘9‘), (‘23‘, ‘6‘, ‘2‘, ‘100‘), (‘24‘, ‘6‘, ‘3‘, ‘67‘), (‘25‘, ‘6‘, ‘4‘, ‘100‘), (‘26‘, ‘7‘, ‘1‘, ‘9‘), (‘27‘, ‘7‘, ‘2‘, ‘100‘), (‘28‘, ‘7‘, ‘3‘, ‘67‘), (‘29‘, ‘7‘, ‘4‘, ‘88‘), (‘30‘, ‘8‘, ‘1‘, ‘9‘), (‘31‘, ‘8‘, ‘2‘, ‘100‘), (‘32‘, ‘8‘, ‘3‘, ‘67‘), (‘33‘, ‘8‘, ‘4‘, ‘88‘), (‘34‘, ‘9‘, ‘1‘, ‘91‘), (‘35‘, ‘9‘, ‘2‘, ‘88‘), (‘36‘, ‘9‘, ‘3‘, ‘67‘), (‘37‘, ‘9‘, ‘4‘, ‘22‘), (‘38‘, ‘10‘, ‘1‘, ‘90‘), (‘39‘, ‘10‘, ‘2‘, ‘77‘), (‘40‘, ‘10‘, ‘3‘, ‘43‘), (‘41‘, ‘10‘, ‘4‘, ‘87‘), (‘42‘, ‘11‘, ‘1‘, ‘90‘), (‘43‘, ‘11‘, ‘2‘, ‘77‘), (‘44‘, ‘11‘, ‘3‘, ‘43‘), (‘45‘, ‘11‘, ‘4‘, ‘87‘), (‘46‘, ‘12‘, ‘1‘, ‘90‘), (‘47‘, ‘12‘, ‘2‘, ‘77‘), (‘48‘, ‘12‘, ‘3‘, ‘43‘), (‘49‘, ‘12‘, ‘4‘, ‘87‘), (‘52‘, ‘13‘, ‘3‘, ‘87‘);
COMMIT;
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` char(1) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_class` (`class_id`),
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES (‘1‘, ‘男‘, ‘1‘, ‘理解‘), (‘2‘, ‘女‘, ‘1‘, ‘钢蛋‘), (‘3‘, ‘男‘, ‘1‘, ‘张三‘), (‘4‘, ‘男‘, ‘1‘, ‘张一‘), (‘5‘, ‘女‘, ‘1‘, ‘张二‘), (‘6‘, ‘男‘, ‘1‘, ‘张四‘), (‘7‘, ‘女‘, ‘2‘, ‘铁锤‘), (‘8‘, ‘男‘, ‘2‘, ‘李三‘), (‘9‘, ‘男‘, ‘2‘, ‘李一‘), (‘10‘, ‘女‘, ‘2‘, ‘李二‘), (‘11‘, ‘男‘, ‘2‘, ‘李四‘), (‘12‘, ‘女‘, ‘3‘, ‘如花‘), (‘13‘, ‘男‘, ‘3‘, ‘刘三‘), (‘14‘, ‘男‘, ‘3‘, ‘刘一‘), (‘15‘, ‘女‘, ‘3‘, ‘刘二‘), (‘16‘, ‘男‘, ‘3‘, ‘刘四‘);
COMMIT;
-- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES (‘1‘, ‘张磊老师‘), (‘2‘, ‘李平老师‘), (‘3‘, ‘刘海燕老师‘), (‘4‘, ‘朱云海老师‘), (‘5‘, ‘李杰老师‘);
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
导入数据
#准备表、记录
mysql> create database db1;
mysql> use db1;
mysql> source /root/init.sql
#1、查询所有的课程的名称以及对应的任课老师姓名
mysql> select * from course c inner join teacher t on c.teacher_id=t.tid;
+-----+--------+------------+-----+-----------------+
| cid | cname | teacher_id | tid | tname |
+-----+--------+------------+-----+-----------------+
| 1 | 生物 | 1 | 1 | 张磊老师 |
| 2 | 物理 | 2 | 2 | 李平老师 |
| 4 | 美术 | 2 | 2 | 李平老师 |
| 3 | 体育 | 3 | 3 | 刘海燕老师 |
+-----+--------+------------+-----+-----------------+
4 rows in set (0.00 sec)
#2、查询学生表中男女生各有多少人
mysql> select gender,count(*) from student group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 女 | 6 |
| 男 | 10 |
+--------+----------+
2 rows in set (0.01 sec)
mysql>
#3、查询物理成绩等于100的学生的姓名
方法一:
mysql> select * from score s inner join course c on s.course_id=c.cid where c.cname=‘物理‘ and s.num=100;
+-----+------------+-----------+-----+-----+--------+------------+
| sid | student_id | course_id | num | cid | cname | teacher_id |
+-----+------------+-----------+-----+-----+--------+------------+
| 23 | 6 | 2 | 100 | 2 | 物理 | 2 |
| 27 | 7 | 2 | 100 | 2 | 物理 | 2 |
| 31 | 8 | 2 | 100 | 2 | 物理 | 2 |
+-----+------------+-----------+-----+-----+--------+------------+
3 rows in set (0.00 sec)
#子查询最终结果
mysql> select * from student where sid in(
select s.student_id from score s inner join course c on s.course_id=c.cid where c.cname=‘物理‘ and s.num=100)
-> ;
+-----+--------+----------+--------+
| sid | gender | class_id | sname |
+-----+--------+----------+--------+
| 6 | 男 | 1 | 张四 |
| 7 | 女 | 2 | 铁锤 |
| 8 | 男 | 2 | 李三 |
+-----+--------+----------+--------+
3 rows in set (0.01 sec)
mysql>
#连接最终结果
mysql> select * from student as stu inner join(
select * from score s inner join course c on s.course_id=c.cid where c.cname=‘物理‘ and s.num=100) as A
on stu.sid= A.student_id
-> ;
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| sid | gender | class_id | sname | sid | student_id | course_id | num | cid | cname | teacher_id |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| 6 | 男 | 1 | 张四 | 23 | 6 | 2 | 100 | 2 | 物理 | 2 |
| 7 | 女 | 2 | 铁锤 | 27 | 7 | 2 | 100 | 2 | 物理 | 2 |
| 8 | 男 | 2 | 李三 | 31 | 8 | 2 | 100 | 2 | 物理 | 2 |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
3 rows in set (0.00 sec)
mysql>
方法三:
mysql> select * from student stu inner join score sco on stu.sid=sco.student_id inner join course as cou on sco.course_id=cou.cid;
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| sid | gender | class_id | sname | sid | student_id | course_id | num | cid | cname | teacher_id |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| 1 | 男 | 1 | 理解 | 1 | 1 | 1 | 10 | 1 | 生物 | 1 |
| 2 | 女 | 1 | 钢蛋 | 6 | 2 | 1 | 8 | 1 | 生物 | 1 |
| 3 | 男 | 1 | 张三 | 10 | 3 | 1 | 77 | 1 | 生物 | 1 |
| 4 | 男 | 1 | 张一 | 14 | 4 | 1 | 79 | 1 | 生物 | 1 |
| 5 | 女 | 1 | 张二 | 18 | 5 | 1 | 79 | 1 | 生物 | 1 |
| 6 | 男 | 1 | 张四 | 22 | 6 | 1 | 9 | 1 | 生物 | 1 |
| 7 | 女 | 2 | 铁锤 | 26 | 7 | 1 | 9 | 1 | 生物 | 1 |
| 8 | 男 | 2 | 李三 | 30 | 8 | 1 | 9 | 1 | 生物 | 1 |
| 9 | 男 | 2 | 李一 | 34 | 9 | 1 | 91 | 1 | 生物 | 1 |
| 10 | 女 | 2 | 李二 | 38 | 10 | 1 | 90 | 1 | 生物 | 1 |
| 11 | 男 | 2 | 李四 | 42 | 11 | 1 | 90 | 1 | 生物 | 1 |
| 12 | 女 | 3 | 如花 | 46 | 12 | 1 | 90 | 1 | 生物 | 1 |
| 1 | 男 | 1 | 理解 | 2 | 1 | 2 | 9 | 2 | 物理 | 2 |
| 3 | 男 | 1 | 张三 | 11 | 3 | 2 | 66 | 2 | 物理 | 2 |
| 4 | 男 | 1 | 张一 | 15 | 4 | 2 | 11 | 2 | 物理 | 2 |
| 5 | 女 | 1 | 张二 | 19 | 5 | 2 | 11 | 2 | 物理 | 2 |
| 6 | 男 | 1 | 张四 | 23 | 6 | 2 | 100 | 2 | 物理 | 2 |
| 7 | 女 | 2 | 铁锤 | 27 | 7 | 2 | 100 | 2 | 物理 | 2 |
| 8 | 男 | 2 | 李三 | 31 | 8 | 2 | 100 | 2 | 物理 | 2 |
| 9 | 男 | 2 | 李一 | 35 | 9 | 2 | 88 | 2 | 物理 | 2 |
| 10 | 女 | 2 | 李二 | 39 | 10 | 2 | 77 | 2 | 物理 | 2 |
| 11 | 男 | 2 | 李四 | 43 | 11 | 2 | 77 | 2 | 物理 | 2 |
| 12 | 女 | 3 | 如花 | 47 | 12 | 2 | 77 | 2 | 物理 | 2 |
| 2 | 女 | 1 | 钢蛋 | 8 | 2 | 3 | 68 | 3 | 体育 | 3 |
| 3 | 男 | 1 | 张三 | 12 | 3 | 3 | 87 | 3 | 体育 | 3 |
| 4 | 男 | 1 | 张一 | 16 | 4 | 3 | 67 | 3 | 体育 | 3 |
| 5 | 女 | 1 | 张二 | 20 | 5 | 3 | 67 | 3 | 体育 | 3 |
| 6 | 男 | 1 | 张四 | 24 | 6 | 3 | 67 | 3 | 体育 | 3 |
| 7 | 女 | 2 | 铁锤 | 28 | 7 | 3 | 67 | 3 | 体育 | 3 |
| 8 | 男 | 2 | 李三 | 32 | 8 | 3 | 67 | 3 | 体育 | 3 |
| 9 | 男 | 2 | 李一 | 36 | 9 | 3 | 67 | 3 | 体育 | 3 |
| 10 | 女 | 2 | 李二 | 40 | 10 | 3 | 43 | 3 | 体育 | 3 |
| 11 | 男 | 2 | 李四 | 44 | 11 | 3 | 43 | 3 | 体育 | 3 |
| 12 | 女 | 3 | 如花 | 48 | 12 | 3 | 43 | 3 | 体育 | 3 |
| 13 | 男 | 3 | 刘三 | 52 | 13 | 3 | 87 | 3 | 体育 | 3 |
| 1 | 男 | 1 | 理解 | 5 | 1 | 4 | 66 | 4 | 美术 | 2 |
| 2 | 女 | 1 | 钢蛋 | 9 | 2 | 4 | 99 | 4 | 美术 | 2 |
| 3 | 男 | 1 | 张三 | 13 | 3 | 4 | 99 | 4 | 美术 | 2 |
| 4 | 男 | 1 | 张一 | 17 | 4 | 4 | 100 | 4 | 美术 | 2 |
| 5 | 女 | 1 | 张二 | 21 | 5 | 4 | 100 | 4 | 美术 | 2 |
| 6 | 男 | 1 | 张四 | 25 | 6 | 4 | 100 | 4 | 美术 | 2 |
| 7 | 女 | 2 | 铁锤 | 29 | 7 | 4 | 88 | 4 | 美术 | 2 |
| 8 | 男 | 2 | 李三 | 33 | 8 | 4 | 88 | 4 | 美术 | 2 |
| 9 | 男 | 2 | 李一 | 37 | 9 | 4 | 22 | 4 | 美术 | 2 |
| 10 | 女 | 2 | 李二 | 41 | 10 | 4 | 87 | 4 | 美术 | 2 |
| 11 | 男 | 2 | 李四 | 45 | 11 | 4 | 87 | 4 | 美术 | 2 |
| 12 | 女 | 3 | 如花 | 49 | 12 | 4 | 87 | 4 | 美术 | 2 |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
47 rows in set (0.00 sec)
mysql> select * from student stu inner join score sco on stu.sid=sco.student_id inner join course as cou on sco.course_id=cou.cid where cname=‘物理‘;
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| sid | gender | class_id | sname | sid | student_id | course_id | num | cid | cname | teacher_id |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| 1 | 男 | 1 | 理解 | 2 | 1 | 2 | 9 | 2 | 物理 | 2 |
| 3 | 男 | 1 | 张三 | 11 | 3 | 2 | 66 | 2 | 物理 | 2 |
| 4 | 男 | 1 | 张一 | 15 | 4 | 2 | 11 | 2 | 物理 | 2 |
| 5 | 女 | 1 | 张二 | 19 | 5 | 2 | 11 | 2 | 物理 | 2 |
| 6 | 男 | 1 | 张四 | 23 | 6 | 2 | 100 | 2 | 物理 | 2 |
| 7 | 女 | 2 | 铁锤 | 27 | 7 | 2 | 100 | 2 | 物理 | 2 |
| 8 | 男 | 2 | 李三 | 31 | 8 | 2 | 100 | 2 | 物理 | 2 |
| 9 | 男 | 2 | 李一 | 35 | 9 | 2 | 88 | 2 | 物理 | 2 |
| 10 | 女 | 2 | 李二 | 39 | 10 | 2 | 77 | 2 | 物理 | 2 |
| 11 | 男 | 2 | 李四 | 43 | 11 | 2 | 77 | 2 | 物理 | 2 |
| 12 | 女 | 3 | 如花 | 47 | 12 | 2 | 77 | 2 | 物理 | 2 |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
11 rows in set (0.00 sec)
#三表连接 最终结果
mysql> select * from student stu inner join score sco on stu.sid=sco.student_id inner join course as cou on sco.course_id=cou.cid where cname=‘物理‘ and num = 100;
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| sid | gender | class_id | sname | sid | student_id | course_id | num | cid | cname | teacher_id |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
| 6 | 男 | 1 | 张四 | 23 | 6 | 2 | 100 | 2 | 物理 | 2 |
| 7 | 女 | 2 | 铁锤 | 27 | 7 | 2 | 100 | 2 | 物理 | 2 |
| 8 | 男 | 2 | 李三 | 31 | 8 | 2 | 100 | 2 | 物理 | 2 |
+-----+--------+----------+--------+-----+------------+-----------+-----+-----+--------+------------+
3 rows in set (0.00 sec)
mysql>
#4、查询平均成绩大于八十分的同学的姓名和平均成绩
mysql> select * from student stu inner join score sco on stu.sid = sco.student_id;
+-----+--------+----------+--------+-----+------------+-----------+-----+
| sid | gender | class_id | sname | sid | student_id | course_id | num |
+-----+--------+----------+--------+-----+------------+-----------+-----+
| 1 | 男 | 1 | 理解 | 1 | 1 | 1 | 10 |
| 1 | 男 | 1 | 理解 | 2 | 1 | 2 | 9 |
| 1 | 男 | 1 | 理解 | 5 | 1 | 4 | 66 |
| 2 | 女 | 1 | 钢蛋 | 6 | 2 | 1 | 8 |
| 2 | 女 | 1 | 钢蛋 | 8 | 2 | 3 | 68 |
| 2 | 女 | 1 | 钢蛋 | 9 | 2 | 4 | 99 |
| 3 | 男 | 1 | 张三 | 10 | 3 | 1 | 77 |
| 3 | 男 | 1 | 张三 | 11 | 3 | 2 | 66 |
| 3 | 男 | 1 | 张三 | 12 | 3 | 3 | 87 |
| 3 | 男 | 1 | 张三 | 13 | 3 | 4 | 99 |
| 4 | 男 | 1 | 张一 | 14 | 4 | 1 | 79 |
| 4 | 男 | 1 | 张一 | 15 | 4 | 2 | 11 |
| 4 | 男 | 1 | 张一 | 16 | 4 | 3 | 67 |
| 4 | 男 | 1 | 张一 | 17 | 4 | 4 | 100 |
| 5 | 女 | 1 | 张二 | 18 | 5 | 1 | 79 |
| 5 | 女 | 1 | 张二 | 19 | 5 | 2 | 11 |
| 5 | 女 | 1 | 张二 | 20 | 5 | 3 | 67 |
| 5 | 女 | 1 | 张二 | 21 | 5 | 4 | 100 |
| 6 | 男 | 1 | 张四 | 22 | 6 | 1 | 9 |
| 6 | 男 | 1 | 张四 | 23 | 6 | 2 | 100 |
| 6 | 男 | 1 | 张四 | 24 | 6 | 3 | 67 |
| 6 | 男 | 1 | 张四 | 25 | 6 | 4 | 100 |
| 7 | 女 | 2 | 铁锤 | 26 | 7 | 1 | 9 |
| 7 | 女 | 2 | 铁锤 | 27 | 7 | 2 | 100 |
| 7 | 女 | 2 | 铁锤 | 28 | 7 | 3 | 67 |
| 7 | 女 | 2 | 铁锤 | 29 | 7 | 4 | 88 |
| 8 | 男 | 2 | 李三 | 30 | 8 | 1 | 9 |
| 8 | 男 | 2 | 李三 | 31 | 8 | 2 | 100 |
| 8 | 男 | 2 | 李三 | 32 | 8 | 3 | 67 |
| 8 | 男 | 2 | 李三 | 33 | 8 | 4 | 88 |
| 9 | 男 | 2 | 李一 | 34 | 9 | 1 | 91 |
| 9 | 男 | 2 | 李一 | 35 | 9 | 2 | 88 |
| 9 | 男 | 2 | 李一 | 36 | 9 | 3 | 67 |
| 9 | 男 | 2 | 李一 | 37 | 9 | 4 | 22 |
| 10 | 女 | 2 | 李二 | 38 | 10 | 1 | 90 |
| 10 | 女 | 2 | 李二 | 39 | 10 | 2 | 77 |
| 10 | 女 | 2 | 李二 | 40 | 10 | 3 | 43 |
| 10 | 女 | 2 | 李二 | 41 | 10 | 4 | 87 |
| 11 | 男 | 2 | 李四 | 42 | 11 | 1 | 90 |
| 11 | 男 | 2 | 李四 | 43 | 11 | 2 | 77 |
| 11 | 男 | 2 | 李四 | 44 | 11 | 3 | 43 |
| 11 | 男 | 2 | 李四 | 45 | 11 | 4 | 87 |
| 12 | 女 | 3 | 如花 | 46 | 12 | 1 | 90 |
| 12 | 女 | 3 | 如花 | 47 | 12 | 2 | 77 |
| 12 | 女 | 3 | 如花 | 48 | 12 | 3 | 43 |
| 12 | 女 | 3 | 如花 | 49 | 12 | 4 | 87 |
| 13 | 男 | 3 | 刘三 | 52 | 13 | 3 | 87 |
+-----+--------+----------+--------+-----+------------+-----------+-----+
47 rows in set (0.00 sec)
#最终结果
mysql> select sname,avg(num) from student stu inner join score sco on stu.sid = sco.student_id group by sname having avg(num)>80;
+--------+----------+
| sname | avg(num) |
+--------+----------+
| 刘三 | 87.0000 |
| 张三 | 82.2500 |
+--------+----------+
2 rows in set (0.00 sec)
mysql>
#5、查询所有学生的学号,姓名,选课数,总成绩(注意:对于那些没有选修任何课程的学生也算在内)
mysql> select student_id,sum(num) total_score,count(course_id) sum_course from score group by student_id
-> ;
+------------+-------------+------------+
| student_id | total_score | sum_course |
+------------+-------------+------------+
| 1 | 85 | 3 |
| 2 | 175 | 3 |
| 3 | 329 | 4 |
| 4 | 257 | 4 |
| 5 | 257 | 4 |
| 6 | 276 | 4 |
| 7 | 264 | 4 |
| 8 | 264 | 4 |
| 9 | 268 | 4 |
| 10 | 297 | 4 |
| 11 | 297 | 4 |
| 12 | 297 | 4 |
| 13 | 87 | 1 |
+------------+-------------+------------+
13 rows in set (0.00 sec)
mysql> select * from student as A left join(select student_id,sum(num) total_score,count(course_id) sum_course from score group by student_id) as B on A.sid=B.student_id
-> ;
+-----+--------+----------+--------+------------+-------------+------------+
| sid | gender | class_id | sname | student_id | total_score | sum_course |
+-----+--------+----------+--------+------------+-------------+------------+
| 1 | 男 | 1 | 理解 | 1 | 85 | 3 |
| 2 | 女 | 1 | 钢蛋 | 2 | 175 | 3 |
| 3 | 男 | 1 | 张三 | 3 | 329 | 4 |
| 4 | 男 | 1 | 张一 | 4 | 257 | 4 |
| 5 | 女 | 1 | 张二 | 5 | 257 | 4 |
| 6 | 男 | 1 | 张四 | 6 | 276 | 4 |
| 7 | 女 | 2 | 铁锤 | 7 | 264 | 4 |
| 8 | 男 | 2 | 李三 | 8 | 264 | 4 |
| 9 | 男 | 2 | 李一 | 9 | 268 | 4 |
| 10 | 女 | 2 | 李二 | 10 | 297 | 4 |
| 11 | 男 | 2 | 李四 | 11 | 297 | 4 |
| 12 | 女 | 3 | 如花 | 12 | 297 | 4 |
| 13 | 男 | 3 | 刘三 | 13 | 87 | 1 |
| 14 | 男 | 3 | 刘一 | NULL | NULL | NULL |
| 15 | 女 | 3 | 刘二 | NULL | NULL | NULL |
| 16 | 男 | 3 | 刘四 | NULL | NULL | NULL |
+-----+--------+----------+--------+------------+-------------+------------+
16 rows in set (0.00 sec)
mysql>
#6、 查询姓李老师的个数
mysql> select * from teacher where tname like ‘李%‘;
+-----+--------------+
| tid | tname |
+-----+--------------+
| 2 | 李平老师 |
| 5 | 李杰老师 |
+-----+--------------+
2 rows in set (0.00 sec)
mysql>
mysql> select count(*) from teacher where tname like ‘李%‘;
+----------+
| count(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
mysql>
#7、 查询没有报李平老师课的学生姓名
mysql> select distinct student_id from score where course_id in(select cid from course where teacher_id=(select tid from teacher where tname=‘李平老师‘));
+------------+
| student_id |
+------------+
| 1 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 2 |
+------------+
12 rows in set (0.00 sec)
mysql>
#最终答案
mysql> select * from student where sid not in(
select distinct student_id from score where course_id in(
select cid frrom course where teacher_id=(select tid from teacher where tname=‘李平老师‘)));
+-----+--------+----------+--------+
| sid | gender | class_id | sname |
+-----+--------+----------+--------+
| 13 | 男 | 3 | 刘三 |
| 14 | 男 | 3 | 刘一 |
| 15 | 女 | 3 | 刘二 |
| 16 | 男 | 3 | 刘四 |
+-----+--------+----------+--------+
4 rows in set (0.00 sec)
#8、 查询物理课程比生物课程高的学生的学号(分别得到物理成绩表与生物成绩表,然后连表即可)
mysql> select * from score inner join course on score.course_id=course.cid where cname=‘物理‘;
+-----+------------+-----------+-----+-----+--------+------------+
| sid | student_id | course_id | num | cid | cname | teacher_id |
+-----+------------+-----------+-----+-----+--------+------------+
| 2 | 1 | 2 | 9 | 2 | 物理 | 2 |
| 11 | 3 | 2 | 66 | 2 | 物理 | 2 |
| 15 | 4 | 2 | 11 | 2 | 物理 | 2 |
| 19 | 5 | 2 | 11 | 2 | 物理 | 2 |
| 23 | 6 | 2 | 100 | 2 | 物理 | 2 |
| 27 | 7 | 2 | 100 | 2 | 物理 | 2 |
| 31 | 8 | 2 | 100 | 2 | 物理 | 2 |
| 35 | 9 | 2 | 88 | 2 | 物理 | 2 |
| 39 | 10 | 2 | 77 | 2 | 物理 | 2 |
| 43 | 11 | 2 | 77 | 2 | 物理 | 2 |
| 47 | 12 | 2 | 77 | 2 | 物理 | 2 |
+-----+------------+-----------+-----+-----+--------+------------+
11 rows in set (0.00 sec)
mysql> select * from (select student_id as pysical_sid ,num as pysical_num from score inner join course on score.course_id=course.cid where cname=‘物理‘) A inner join
-> (select student_id as biology_sid ,num as biology_num from score inner join course on score.course_id=course.cid where cname=‘生物‘) B
-> on A.pysical_sid=B.biology_sid
-> ;
+-------------+-------------+-------------+-------------+
| pysical_sid | pysical_num | biology_sid | biology_num |
+-------------+-------------+-------------+-------------+
| 1 | 9 | 1 | 10 |
| 3 | 66 | 3 | 77 |
| 4 | 11 | 4 | 79 |
| 5 | 11 | 5 | 79 |
| 6 | 100 | 6 | 9 |
| 7 | 100 | 7 | 9 |
| 8 | 100 | 8 | 9 |
| 9 | 88 | 9 | 91 |
| 10 | 77 | 10 | 90 |
| 11 | 77 | 11 | 90 |
| 12 | 77 | 12 | 90 |
+-------------+-------------+-------------+-------------+
11 rows in set (0.00 sec)
#最终答案,这里也查出了学生的详细信息
select pysical_sid from
(select student_id as pysical_sid ,num as pysical_num from score inner join course on score.course_id=course.cid where cname=‘物理‘) A
inner join
(select student_id as biology_sid ,num as biology_num from score inner join course on score.course_id=course.cid where cname=‘生物‘) B
on A.pysical_sid=B.biology_sid
where A.pysical_num>B.biology_num
+-----+--------+----------+--------+
| sid | gender | class_id | sname |
+-----+--------+----------+--------+
| 6 | 男 | 1 | 张四 |
| 7 | 女 | 2 | 铁锤 |
| 8 | 男 | 2 | 李三 |
+-----+--------+----------+--------+
3 rows in set (0.00 sec)
mysql>
#9、 查询没有同时选修物理课程和体育课程的学生姓名
#一种理解是除了同时选修物理和体育的学生
mysql> select * from student where sid not in (
select A.student_id from
(select * from score inner join course on course_id=cid where cname=‘物理‘)A
inner join
(select * from score inner join course on course_id=cid where cname=‘体育‘)B
on A.student_id=B.student_id);
+-----+--------+----------+--------+
| sid | gender | class_id | sname |
+-----+--------+----------+--------+
| 1 | 男 | 1 | 理解 |
| 2 | 女 | 1 | 钢蛋 |
| 13 | 男 | 3 | 刘三 |
| 14 | 男 | 3 | 刘一 |
| 15 | 女 | 3 | 刘二 |
| 16 | 男 | 3 | 刘四 |
+-----+--------+----------+--------+
6 rows in set (0.00 sec)
#一种理解是只选修了其中一门的学生
mysql> select student_id from score where course_id in
-> (select cid from course where cname in(‘物理‘,‘体育‘))
-> group by student_id having count(student_id)=1
-> ;
+------------+
| student_id |
+------------+
| 1 |
| 2 |
| 13 |
+------------+
3 rows in set (0.00 sec)
mysql>
#10、查询挂科超过两门(包括两门)的学生姓名和班级(在score表中查询出分数低于60的,然后按照学生分组,count(student_id)>=2的)
mysql> select * from student A
inner join
(select student_id from score where num<60 group by studennt_id having count(course_id)>=2 ) B
on A.sid=B.student_id
inner join class C
on A.class_id=C.cid
-> ;
+-----+--------+----------+--------+------------+-----+--------------+
| sid | gender | class_id | sname | student_id | cid | caption |
+-----+--------+----------+--------+------------+-----+--------------+
| 1 | 男 | 1 | 理解 | 1 | 1 | 三年二班 |
+-----+--------+----------+--------+------------+-----+--------------+
1 row in set (0.00 sec)
mysql>
#11、查询选修了所有课程的学生姓名(在score表按照学生分组,count(student_id)=course表中的课程数)
select * from student A inner join
(select student_id from score group by student_id having count(*)=(select count(*) from course))B
on A.sid=B.student_id;
+-----+--------+----------+--------+------------+
| sid | gender | class_id | sname | student_id |
+-----+--------+----------+--------+------------+
| 3 | 男 | 1 | 张三 | 3 |
| 4 | 男 | 1 | 张一 | 4 |
| 5 | 女 | 1 | 张二 | 5 |
| 6 | 男 | 1 | 张四 | 6 |
| 7 | 女 | 2 | 铁锤 | 7 |
| 8 | 男 | 2 | 李三 | 8 |
| 9 | 男 | 2 | 李一 | 9 |
| 10 | 女 | 2 | 李二 | 10 |
| 11 | 男 | 2 | 李四 | 11 |
| 12 | 女 | 3 | 如花 | 12 |
+-----+--------+----------+--------+------------+
10 rows in set (0.00 sec)
#12、查询李平老师教的课程的所有成绩记录
mysql> select * from score where course_id in
(select cid from course where teacher_id =(select tid from teacher where tname=‘李平老师‘));
+-----+------------+-----------+-----+
| sid | student_id | course_id | num |
+-----+------------+-----------+-----+
| 2 | 1 | 2 | 9 |
| 11 | 3 | 2 | 66 |
| 15 | 4 | 2 | 11 |
| 19 | 5 | 2 | 11 |
| 23 | 6 | 2 | 100 |
| 27 | 7 | 2 | 100 |
| 31 | 8 | 2 | 100 |
| 35 | 9 | 2 | 88 |
| 39 | 10 | 2 | 77 |
| 43 | 11 | 2 | 77 |
| 47 | 12 | 2 | 77 |
| 5 | 1 | 4 | 66 |
| 9 | 2 | 4 | 99 |
| 13 | 3 | 4 | 99 |
| 17 | 4 | 4 | 100 |
| 21 | 5 | 4 | 100 |
| 25 | 6 | 4 | 100 |
| 29 | 7 | 4 | 88 |
| 33 | 8 | 4 | 88 |
| 37 | 9 | 4 | 22 |
| 41 | 10 | 4 | 87 |
| 45 | 11 | 4 | 87 |
| 49 | 12 | 4 | 87 |
+-----+------------+-----------+-----+
23 rows in set (0.00 sec)
mysql>
#13、查询全部学生都选修了的课程号和课程名(取所有学生数,然后基于score表的课程分组,找出count(student_id)等于学生数即可)
mysql> select cid,cname from course where cid in
-> (select course_id from score group by course_id having count(student_id)=
-> (select count(*) from student));
Empty set (0.00 sec)
mysql>
#14、查询每门课程被选修的次数
mysql> select course_id,count(student_id) from score group by course_id;
+-----------+-------------------+
| course_id | count(student_id) |
+-----------+-------------------+
| 1 | 12 |
| 2 | 11 |
| 3 | 12 |
| 4 | 12 |
+-----------+-------------------+
4 rows in set (0.00 sec)
mysql>
#15、查询只选修了一门课程的学生姓名和学号
mysql> select * from student where sid in
(select student_id from score group by student_id having count(course_id)=1);
+-----+--------+----------+--------+
| sid | gender | class_id | sname |
+-----+--------+----------+--------+
| 13 | 男 | 3 | 刘三 |
+-----+--------+----------+--------+
1 row in set (0.00 sec)
mysql>
#16、查询所有学生考出的成绩并按从高到低排序(成绩去重)
mysql> select distinct num from score order by num desc;
+-----+
| num |
+-----+
| 100 |
| 99 |
| 91 |
| 90 |
| 88 |
| 87 |
| 79 |
| 77 |
| 68 |
| 67 |
| 66 |
| 43 |
| 22 |
| 11 |
| 10 |
| 9 |
| 8 |
+-----+
17 rows in set (0.00 sec)
mysql>
#17、查询平均成绩大于85的学生姓名和平均成绩
mysql> select * from student as A
-> inner join
-> (select student_id,avg(num) from score group by student_id having avg(num)>85) as B
-> on A.sid=B.student_id;
+-----+--------+----------+--------+------------+----------+
| sid | gender | class_id | sname | student_id | avg(num) |
+-----+--------+----------+--------+------------+----------+
| 13 | 男 | 3 | 刘三 | 13 | 87.0000 |
+-----+--------+----------+--------+------------+----------+
1 row in set (0.00 sec)
mysql>
#18、查询生物成绩不及格的学生姓名和对应生物分数
mysql> select * from score A inner join course B on A.course_id=B.cid where B.cname=‘生物‘ and A.num<60;
+-----+------------+-----------+-----+-----+--------+------------+
| sid | student_id | course_id | num | cid | cname | teacher_id |
+-----+------------+-----------+-----+-----+--------+------------+
| 1 | 1 | 1 | 10 | 1 | 生物 | 1 |
| 6 | 2 | 1 | 8 | 1 | 生物 | 1 |
| 22 | 6 | 1 | 9 | 1 | 生物 | 1 |
| 26 | 7 | 1 | 9 | 1 | 生物 | 1 |
| 30 | 8 | 1 | 9 | 1 | 生物 | 1 |
+-----+------------+-----------+-----+-----+--------+------------+
5 rows in set (0.00 sec)
select sname,C.num from student stu inner join
(select * from score A inner join course B on A.course_id=B.cid where B.cname=‘生物‘ and A.num<60) C
on stu.sid=C.student_id;
+--------+-----+
| sname | num |
+--------+-----+
| 理解 | 10 |
| 钢蛋 | 8 |
| 张四 | 9 |
| 铁锤 | 9 |
| 李三 | 9 |
+--------+-----+
5 rows in set (0.00 sec)
#19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
mysql> select student_id,avg(num) from score where course_id in
-> (select cid from course inner join teacher on teacher_id=tid where tname=‘李平老师‘)
-> group by student_id;
+------------+----------+
| student_id | avg(num) |
+------------+----------+
| 1 | 37.5000 |
| 2 | 99.0000 |
| 3 | 82.5000 |
| 4 | 55.5000 |
| 5 | 55.5000 |
| 6 | 100.0000 |
| 7 | 94.0000 |
| 8 | 94.0000 |
| 9 | 55.0000 |
| 10 | 82.0000 |
| 11 | 82.0000 |
| 12 | 82.0000 |
+------------+----------+
12 rows in set (0.00 sec)
select * from student A
inner join (select student_id,avg(num) avg_num from score where course_id in
(select cid from course inner join teacher on teacher_id=tid where tname=‘李平老师‘)
group by student_id order by avg_num desc limit 1)B
on A.sid=B.student_id;
+-----+--------+----------+--------+------------+----------+
| sid | gender | class_id | sname | student_id | avg_num |
+-----+--------+----------+--------+------------+----------+
| 6 | 男 | 1 | 张四 | 6 | 100.0000 |
+-----+--------+----------+--------+------------+----------+
1 row in set (0.00 sec)
#20、查询每门课程成绩最好的前两名学生姓名
#21、查询不同课程但成绩相同的学号,课程号,成绩
#22、查询没学过“叶平”老师课程的学生姓名以及选修的课程名称;
#23、查询所有选修了学号为1的同学选修过的一门或者多门课程的同学学号和姓名;
#24、任课最多的老师中学生单科成绩最高的学生姓名
以上是关于mysql续集5-多表查询练习的主要内容,如果未能解决你的问题,请参考以下文章