MySQL多表查询练习

一、准备数据

#创建表及插入记录

CREATE TABLE class (  
cid int(11) NOT NULL AUTO_INCREMENT,  
caption varchar(32) NOT NULL,  
PRIMARY KEY (cid))
 ENGINE=InnoDB CHARSET=utf8;

导入数据

INSERT INTO class VALUES
(1, '三年二班'), (2, '三年三班'), (3, '一年二班'), (4, '二年九班');

创建表

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 DEFAULT CHARSET=utf8;

导入数据

INSERT INTO course VALUES
(1, '生物', 1), (2, '物理', 2), (3, '体育', 3), (4, '美术', 2);

创建表

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 DEFAULT CHARSET=utf8;

导入数据

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);

创建表

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 DEFAULT CHARSET=utf8;

导入数据

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, '刘四');

创建表

CREATE TABLE teacher(  
tid int(11) NOT NULL AUTO_INCREMENT,  
tname varchar(32) NOT NULL,  
PRIMARY KEY (tid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

导入数据

INSERT INTO teacher VALUES
(1, '张磊老师'), (2, '李平老师'), (3, '刘海燕老师'), 
(4, '朱云海老师'), (5, '李杰老师');

二、题目一

1、查询所有的课程的名称以及对应的任课老师姓名

select cname,tname from course left join teacher on teacher_id = tid;

2、查询学生表中男女生各有多少人

select gender,count(*) num from student group by gender;

3、查询物理成绩等于100的学生的姓名

select sname from score join course join student on 
course_id = cid and student_id = student.sid 
where cname = '物理' and num = 100;

4、查询平均成绩大于八十分的同学的姓名和平均成绩

select sname,avg_num from student 
join (select student_id, avg(num) avg_num from score 
group by student_id having avg(num) > 80) 
as t2 on sid = t2.student_id;

5、查询所有学生的学号,姓名,选课数,总成绩

select student.sid,sname,course_num,
total_mark from student join 
(select student_id,count(*) course_num,sum(num) 
total_mark from score group by student_id) 
as t on sid = student_id;

6、 查询姓李老师的个数

select count(*) num from teacher where tname like '李%';  

7、 查询没有报李平老师课的学生姓名

select sname from student where sid not in 
(select student_id from score where course_id in 
(select cid from course join teacher on tid = teacher_id 
where tname = '李平老师'));

8、 查询物理课程比生物课程高的学生的学号 #

select * from
        (select score.student_id,course.cname,score.num from score left join 
course on score.course_id=course.cid WHERE course.cname="生物") AS A
     inner join
        (select score.student_id,course.cname,score.num from score left join 
course on score.course_id=course.cid WHERE course.cname="物理") AS B
        on A.student_id = B.student_id WHERE A.num > B.num;

9、 查询没有同时选修物理课程和体育课程的学生姓名

select sname from student where sid not in 
(select w.student_id from 
(select student_id from score where course_id =
(select cid from course where cname = '物理')) 
as w join(select student_id from score 
where course_id =(select cid from course where cname = '体育'))
 as s on w.student_id = s.student_id); 

10、查询挂科超过两门(包括两门)的学生姓名和班级

select sname,caption from student join 
(select student_id from score 
where num < 60 group by student_id having count(*) >=2) 
as s join class on class_id = cid and student_id = sid;

11、查询选修了所有课程的学生姓名

select sname from score join student on student_id = student.sid 
group by student_id having count(student_id) = (select count(cid) 
course_num from course); 

12、查询李平老师教的课程的所有成绩记录

select * from score where course_id in 
(select cid from teacher join course
on tid = teacher_id where tname = '李平老师');

13、查询全部学生都选修了的课程号和课程名

select course_id,cname from score join course on course_id = cid 
group by course_id having count(*) = (select count(sid) from student);

14、查询每门课程被选修的次数

select cname,count(course_id) from score join 
course on cid = course_id group by course_id;

15、查询之选修了一门课程的学生姓名和学号

select student_id,sname from score 
join student on student.sid = student_id 
group by student_id having count(student_id) = 1;

16、查询所有学生考出的成绩并按从高到低排序(成绩去重)

select distinct num from score order by num desc;

17、查询平均成绩大于85的学生姓名和平均成绩

select sname,t.avg_num from student join  
(select student_id, avg(num) avg_num from score 
group by student_id having avg(num) > 85) as t on 
student.sid = t.student_id;

18、查询生物成绩不及格的学生姓名和对应生物分数

select sname,t.num from student join 
(select student_id,num from score join 
course on course_id = cid where num < 60 and cname = '生物')
 as t on student.sid = t.student_id;

19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名

select sname from student join
(select student_id,avg(num) from score where
 course_id in (select cid from teacher join course 
on tid = teacher_id where tname = '李平老师') group by 
student_id order by avg(num) desc limit 1) 
as t on student.sid = student_id; 

进阶练习一:

创建库 create database 数据库名字

创建表

CREATE TABLE t_dept ( 
id INT(11) NOT NULL AUTO_INCREMENT, 
deptName VARCHAR(30) DEFAULT NULL, 
address VARCHAR(40) DEFAULT NULL, 
PRIMARY KEY (id)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE t_emp ( 
id INT(11) NOT NULL AUTO_INCREMENT, 
name VARCHAR(20) DEFAULT NULL,  
age INT(3) DEFAULT NULL, 
deptId INT(11) DEFAULT NULL,
empno int  not null, 
PRIMARY KEY (id), 
KEY idx_dept_id (deptId) 
#CONSTRAINT fk_dept_id FOREIGN KEY (deptId) REFERENCES t_dept (id)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

创建数据

INSERT INTO t_dept(deptName,address) VALUES
('华山','华山');
INSERT INTO t_dept(deptName,address) VALUES
('丐帮','洛阳');
INSERT INTO t_dept(deptName,address) VALUES
('峨眉','峨眉山');
INSERT INTO t_dept(deptName,address) VALUES
('武当','武当山');
INSERT INTO t_dept(deptName,address) VALUES
('明教','光明顶');
INSERT INTO t_dept(deptName,address) VALUES
('少林','少林寺');
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('风清扬',90,1,100001);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('岳不群',50,1,100002);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('令狐冲',24,1,100003);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('洪七公',70,2,100004);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('乔峰',35,2,100005);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('灭绝师太',70,3,100006);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('周芷若',20,3,100007);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('张三丰',100,4,100008);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('张无忌',25,5,100009);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES
('韦小宝',18,null,100010);

ALTER TABLE t_dept 
add  CEO  INT(11)  ;

CEO=2 值,都应该是t_emp 中id的值。

update t_dept set CEO=2 where id=1;
update t_dept set CEO=4 where id=2;
update t_dept set CEO=6 where id=3;
update t_dept set CEO=8 where id=4;
update t_dept set CEO=9 where id=5;

下面来看几个问题:

1 所有有门派的人员信息

select *from t_emp a inner join t_dept b on a.deptId = b.id;

2 列出所有用户,并显示其机构信息

select *from t_emp a left join t_dept b on a.deptId = b.id;

3 列出不入派的人员:

select * from t_emp a left join t_dept b on a.deptId = b.id where b.id is null;

4 所有没人入的门派 :

select * from t_emp b left join t_dept b on a.deptId = b.id where b.id is null;

5 列出所有人员和门派的对照关系

select * from t_dept b left join t_emp a on a.deptId = b.id where a.deptId is null;

6 列出所有没入派的人员和没人入的门派

SELECT * from t_emp left JOIN t_dept on t_emp.deptId = t_dept.id where t_dept.id is null;

7 求各个门派对应的掌门人名称:

select a.name from t_dept b left join t_emp a on b.CEO=a.id;

8 求所有当上掌门人的平均年龄:

SELECT AVG(e.age) FROM t_dept d LEFT JOIN t_emp e ON d.ceo=e.id;

9 求所有人物对应的掌门名称:

SELECT ed.name '人物',c.name '掌门' FROM 
(SELECT e.name,d.ceo from t_emp e LEFT JOIN t_dept d on e.deptid=d.id) 
ed LEFT JOIN t_emp c on ed.ceo= c.id;

进阶练习二:

CREATE TABLE course (

cid varchar(50) NOT NULL,

cname varchar(50) NOT NULL,

tid int(10) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO course VALUES ('001', 'PHP', '1');

INSERT INTO course VALUES ('002', 'C', '1');

INSERT INTO course VALUES ('003', 'C++', '2');

INSERT INTO course VALUES ('004', 'JAVA', '3');

INSERT INTO course VALUES ('005', 'python', '4');

INSERT INTO course VALUES ('006', 'R', '5');

INSERT INTO course VALUES ('007', 'HTML', '6');

CREATE TABLE sc (

sid int(10) NOT NULL,

cid varchar(50) NOT NULL,

score int(10) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO sc VALUES ('1001', '001', '89');

INSERT INTO sc VALUES ('1002', '001', '80');

INSERT INTO sc VALUES ('1003', '001', '30');

INSERT INTO sc VALUES ('1004', '001', '78');

INSERT INTO sc VALUES ('1005', '001', '68');

INSERT INTO sc VALUES ('1006', '001', '93');

INSERT INTO sc VALUES ('1007', '001', '62');

INSERT INTO sc VALUES ('1001', '002', '67');

INSERT INTO sc VALUES ('1002', '002', '86');

INSERT INTO sc VALUES ('1003', '002', '67');

INSERT INTO sc VALUES ('1004', '002', '77');

INSERT INTO sc VALUES ('1006', '002', '84');

INSERT INTO sc VALUES ('1007', '002', '72');

INSERT INTO sc VALUES ('1001', '003', '82');

INSERT INTO sc VALUES ('1002', '003', '85');

INSERT INTO sc VALUES ('1003', '003', '32');

INSERT INTO sc VALUES ('1004', '003', '73');

INSERT INTO sc VALUES ('1005', '003', '64');

INSERT INTO sc VALUES ('1006', '003', '87');

INSERT INTO sc VALUES ('1007', '003', '77');

INSERT INTO sc VALUES ('1001', '004', '39');

INSERT INTO sc VALUES ('1003', '004', '80');

INSERT INTO sc VALUES ('1004', '004', '88');

INSERT INTO sc VALUES ('1005', '004', '68');

INSERT INTO sc VALUES ('1006', '004', '59');

INSERT INTO sc VALUES ('1007', '004', '42');

INSERT INTO sc VALUES ('1008', '004', '64');

INSERT INTO sc VALUES ('1001', '005', '89');

INSERT INTO sc VALUES ('1002', '005', '70');

INSERT INTO sc VALUES ('1003', '005', '60');

INSERT INTO sc VALUES ('1004', '005', '58');

INSERT INTO sc VALUES ('1005', '005', '38');

INSERT INTO sc VALUES ('1006', '005', '89');

INSERT INTO sc VALUES ('1007', '005', '72');

INSERT INTO sc VALUES ('1008', '005', '64');

INSERT INTO sc VALUES ('1001', '006', '49');

INSERT INTO sc VALUES ('1002', '006', '90');

INSERT INTO sc VALUES ('1003', '006', '70');

INSERT INTO sc VALUES ('1004', '006', '48');

INSERT INTO sc VALUES ('1005', '006', '98');

INSERT INTO sc VALUES ('1006', '006', '59');

INSERT INTO sc VALUES ('1007', '006', '72');

INSERT INTO sc VALUES ('1008', '006', '74');

INSERT INTO sc VALUES ('1001', '007', '49');

INSERT INTO sc VALUES ('1002', '007', '50');

INSERT INTO sc VALUES ('1003', '007', '70');

INSERT INTO sc VALUES ('1004', '007', '88');

INSERT INTO sc VALUES ('1005', '007', '78');

INSERT INTO sc VALUES ('1006', '007', '99');

CREATE TABLE student (

sid int(10) NOT NULL,

sname varchar(50) NOT NULL,

age int(10) NOT NULL,

sex varchar(10) NOT NULL,

PRIMARY KEY (sid)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO student VALUES ('1001', '张宇', '12', '男');

INSERT INTO student VALUES ('1002', '张梅', '18', '女');

INSERT INTO student VALUES ('1003', '王鑫', '16', '男');

INSERT INTO student VALUES ('1004', '马东东', '13', '女');

INSERT INTO student VALUES ('1005', '孙子涵', '12', '男');

INSERT INTO student VALUES ('1006', '钱一', '12', '男');

INSERT INTO student VALUES ('1007', '赵贺', '15', '男');

INSERT INTO student VALUES ('1008', '周雪', '16', '女');

CREATE TABLE teacher (

tid int(10) NOT NULL,

tname varchar(50) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO teacher VALUES ('1', '李雷');

INSERT INTO teacher VALUES ('2', '李纯');

INSERT INTO teacher VALUES ('3', '胡悦悦');

INSERT INTO teacher VALUES ('4', '朱清时');

INSERT INTO teacher VALUES ('5', '赛鸿飞');

INSERT INTO teacher VALUES ('6', '宋三东');

-- 查询平均成绩大于70分的同学的学号和平均成绩1、查询平均成绩大于70分

select sid,avg(score) from sc

group by sid

having avg(score)>70;

-- 查询所有同学的学号、姓名、选课数、总成绩

select * from student;

select * from sc;

select s1.sid,s1.sname,count(s2.sid),sum(s2.score)

from student s1

inner join sc s2

on s1.sid=s2.sid

group by s2.sid

select sid,count(*) from sc

group by sid;

-- 查询姓“李”的老师的个数

select count(1) from teacher

where tname like '李%';

-- 查询学过“李纯”老师课的同学的学号、姓名

select * from student;

select * from course;

select * from sc;

select * from teacher;

连接查询:select a.sid,a.sname

from student a,course b,sc c,teacher d

where a.sid=c.sid

and b.cid=c.cid

and b.tid=d.tid

and d.tname='李纯';

子查询:1、查询“李纯”老师tid

select tid from teacher where tname='李纯';

2、查询“李纯”老师的cid

select cid from course

where tid = (select tid from teacher where tname='李纯');

3、通过sc查询学过“李纯”老师课程的sid

select sid from sc

where cid in (

select cid from course

where tid = (select tid from teacher where tname='李纯')

);

4、查询学生信息

select sid,sname from student

where sid in (

select sid from sc

where cid in (

select cid from course

where tid = (select tid from teacher where tname='李纯')

));

-- 查询没学过“李雷”老师课的同学的学号、姓名

1、查出李雷的tid

select tid from teacher where tname='李雷';

2、查询李雷教的课程cid

select cid from course

where tid=(select tid from teacher where tname='李雷');

3、查询学过李雷课程的sid

select distinct sid from sc

where cid in (

select cid from course

where tid=(select tid from teacher where tname='李雷')

);

4、没学过李雷课程的同学

SELECT * FROM student

WHERE sid NOT IN ( SELECT DISTINCT sid FROM sc WHERE

cid IN ( SELECT cid FROM course WHERE tid =( SELECT tid FROM teacher WHERE tname = '李雷' ) )

);

-- 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名

select * from sc;

select * from student;

1、使用子查询

select s.sid,s.sname from student s

where s.sid in (

select t1.sid from

(select * from sc where cid='001') t1,

(select * from sc where cid='002') t2

where t1.sid = t2.sid);

2、使用连接查询

select s.sid,s.sname from student s,

sc t1,sc t2

where s.sid=t1.sid

and t1.cid='001'

and t2.cid='002'

and t1.sid=t2.sid;

-- 查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名课程001和002的成绩

select s.sid,s.sname from student s

where s.sid in

(select t1.sid from

(select * from sc where cid='001') t1,

(select * from sc where cid='002') t2

where t1.sid = t2.sid and t2.score<t1.score) ;

select s.sid,s.sname from student s,

sc t1,sc t2

where s.sid=t1.sid

and t1.cid='001'

and t2.cid='002

'and t1.sid=t2.sid

and t2.score<t1.score;

-- 查询学过“李雷”老师所教的所有课的同学的学号、姓名

1、查询李雷老师的tid

select tid from teacher where tname='李雷';

2、查询李雷老师的cid

select cid from course

where tid in (select tid from teacher where tname='李雷');

3、查询学习过李雷老师的学生sid

select distinct sid from sc

where cid in (

select cid from course

where tid in (select tid from teacher where tname='李雷'));

4、查询学过李雷老师的学生的学号姓名

select sid,sname from student

where sid in (

select distinct sid from sc

where cid in (

select cid from course

where tid in (select tid from teacher where tname='李雷')))

-- 查询各科成绩最高和最低的分:以如下形式显示:课程ID,课程名最高分,最低分

1、按cid分组,查询各科的最高和最低分

select cid,max(score),min(score) from sc

group by cid;

2、1的结果和course连接查询

select a.cid,a.cname,b.max,b.min from course a

inner join (select cid,max(score) max,min(score) min from sc

group by cid) b

on a.cid=b.cid;

-- 查询没有学全所有课的同学的学号、姓名

1、统计课程的个数

select count(1) from course;

2、按sid分组,统计每个学生学习了多少课程

select sid,count(1) from sc group by sid;

3、找出没学全部课程的

sidselect sid from sc group by sid

having count(1)< (select count(1) from course);

4、根据3的结果输出学生信息

select sid,sname from student

where sid in (

select sid from sc group by sid

having count(1)< (select count(1) from course));

-- 查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名1、找出学号为1001的同学学习的课程cid

select cid from sc

where sid=1001;

2、找出至少学习过1结果中的任意一门课程的学生的sid

select distinct sid from sc

where cid in (

select cid from scwhere sid=1001) and sid!=1001;

3、根据2的结果输出学生信息

select sid,sname from student

where sid in(

select distinct sid from sc

where cid in (

select cid from scwhere sid=1001

) and sid!=1001);

-- 按各科平均成绩从低到高和及格率的百分数从高到低顺序。补充:concat()

-- 方法用于连接两个或多个数组,百分数表示方法 CONCAT(值1/值2*100,'%')

计算各科的及格率:及格人数/总人数

计算各科及格人数:

select cid cid2,count(1) c2 from sc

where score>=60 group by cid;

select s1.cid,avg(s1.score) avg_sc,concat(t.c2/count(s1.score)*100,'%') rate

from sc s1

left join (

select cid cid2,count(1) c2 from sc

where score>=60 group by cid

) t

on s1.cid = t.cid2

group by s1.cid

order by avg_sc,rate desc;

-- 删除学习“李纯”老师课的SC表记录

1、“李纯”老师的cid

select cid from teacher t,course c where t.tid=c.tid and t.tname = '李纯';

2、根据cid删除SC表记录

delete from sc

where cid in (

select cid from teacher t,course c where t.tid=c.tid and t.tname = '李纯'

);

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值