Mysql关联查询
1、数据准备
# 班级表
create table class(id int primary key auto_increment,name varchar(20),description varchar(100)
);
# 学生表
create table student(id int primary key auto_increment,sn varchar(20),name varchar(20),email varchar(20),class_id int,monitor_id int,constraint student_class_id foreign key (class_id) references class(id)constraint student_monitor_id foreign key (monitor_id) references student(id)
);
# 课程表
create table course(id int primary key auto_increment,name varchar(20)
);
# 成绩表
create table score(student_id int,course_id int,score double,constraint score_student_id foreign key (student_id) references student(id),constraint score_course_id foreign key (course_id) references course(id)
);
2、笛卡尔积
会使用某一张表中的每一条记录都与另外一张表的所有记录进行组合,比如表A有x条记录,表B有y条记录,最终组合数为x*y,常常与where一起使用,此时也被称为等值连接
# 查询同学姓名="xumeng03"的同学的成绩(等值连接)
select s.sn as 学号, s.name as 同学姓名, score.score as 成绩
from student s,score
where s.id = score.student_idand s.name = "xumeng03"# 查询所有同学的个人信息和总成绩
select s.sn as 学号, s.name as 同学姓名, sum(score.score) as 成绩
from student s,score
where s.id = score.student_id
group by 学号;# 查询所有同学的个人信息和各科成绩
select s.sn as 学号, s.name as 同学姓名, c.name as 课程, score.score as 成绩
from student s,score,course c
where s.id = score.student_idand score.course_id = c.id
order by 学号 asc;# 查询所有同学的的班级
select s.sn as 学号, s.name as 同学姓名, c.name as 班级名称
from student s,class c
where s.class_id = c.id;
3、内连接
返回所有满足条件的记录
# 查询同学姓名="xumeng03"的同学的成绩(内连接)
select s.sn as 学号, s.name as 同学姓名, score.score as 成绩
from student sjoin score on s.id = score.student_id and s.name = "xumeng03"# 查询所有同学的个人信息和总成绩(内连接)
select s.sn as 学号, s.name as 同学姓名, sum(score.score) as 成绩
from student sjoin score on s.id = score.student_id
group by 学号;# 查询所有同学的个人信息和各科成绩(内连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 课程, score.score as 成绩
from student sjoin score on s.id = score.student_idjoin course c on score.course_id = c.id
order by 学号 asc;# 查询所有同学的的班级(内连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 班级名称
from student sjoin study.class c on c.id = s.class_id
where s.class_id = c.id;
4、外连接
驱动表(主表):除了显示满足条件的数据,还需要显示不满足条件的数据的表
从表(副表):只显示满足关联条件的数据的表
- 当关联表中数据数据在两个表中都有体现,则内连接与左外连、右外连接接结果一致
- 多次join on时,每次join on 语句都只计算两个表的笛卡尔积
4.1、左外连接
# 查询所有同学的的班级(左外连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 班级名称
from student sleft join study.class c on c.id = s.class_id
where s.class_id = c.id;# 查询所有同学的个人信息和各科成绩(左外连接,必定展示student中所有内容)
select s.sn as 学号, s.name as 同学姓名, c.name as 课程, score.score as 成绩
from student sleft join score on s.id = score.student_idleft join course c on score.course_id = c.id
order by 学号 asc;
4.2、右外连接
# 查询所有同学的的班级(右外连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 班级名称
from student sright join study.class c on c.id = s.class_id
where s.class_id = c.id;# 查询所有同学的个人信息和各科成绩(右外连接,必定展示course中所有内容)
select s.sn as 学号, s.name as 同学姓名, c.name as 课程, score.score as 成绩
from student sright join score on s.id = score.student_idright join course c on score.course_id = c.id
order by 学号 asc;
5、自连接
# 查询每个同学的班长
select s1.sn as 学号, s1.name as 学生姓名, s2.name 班长姓名
from student s1,student s2
where s1.monitor_id = s2.id
6、子查询
# 查询名称="xumeng03"的同班同学
select sn as 学号, name as 学生姓名
from student
where class_id in (select id from student where name = "xumeng03");
7、合并查询
or只能对一张表的查询结果进行合并,但union可以对多张表的查询结果进行合并(要求多个结果的列须对应)
使用union关键字对多个查询结果进行合并时会自动去重,但union all不会去重
# 查询名称="xumeng03"的同学和他的班长
select *
from student
where name = "xumeng03"
union
select *
from student
where id = (select monitor_id from student where name = "xumeng03")