Mysql关联查询

Mysql关联查询

1、数据准备

sql 复制代码
# 班级表
create table class(
    id int primary key auto_increment,
    name varchar(20),
    description varchar(100)
);
sql 复制代码
# 学生表
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)
);
sql 复制代码
# 课程表
create table course(
    id int primary key auto_increment,
    name varchar(20)
);
sql 复制代码
# 成绩表
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一起使用,此时也被称为等值连接

sql 复制代码
# 查询同学姓名="xumeng03"的同学的成绩(等值连接)
select s.sn as 学号, s.name as 同学姓名, score.score as 成绩
from student s,
     score
where s.id = score.student_id
  and 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_id
  and 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、内连接

返回所有满足条件的记录

sql 复制代码
# 查询同学姓名="xumeng03"的同学的成绩(内连接)
select s.sn as 学号, s.name as 同学姓名, score.score as 成绩
from student s
         join score on s.id = score.student_id and s.name = "xumeng03"

# 查询所有同学的个人信息和总成绩(内连接)
select s.sn as 学号, s.name as 同学姓名, sum(score.score) as 成绩
from student s
         join score on s.id = score.student_id
group by 学号;

# 查询所有同学的个人信息和各科成绩(内连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 课程, score.score as 成绩
from student s
         join score on s.id = score.student_id
         join course c on score.course_id = c.id
order by 学号 asc;

# 查询所有同学的的班级(内连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 班级名称
from student s
     join study.class c on c.id = s.class_id
where s.class_id = c.id;

4、外连接

驱动表(主表):除了显示满足条件的数据,还需要显示不满足条件的数据的表

从表(副表):只显示满足关联条件的数据的表

  • 当关联表中数据数据在两个表中都有体现,则内连接与左外连、右外连接接结果一致
  • 多次join on时,每次join on 语句都只计算两个表的笛卡尔积

4.1、左外连接

sql 复制代码
# 查询所有同学的的班级(左外连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 班级名称
from student s
     left 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 s
         left join score on s.id = score.student_id
         left join course c on score.course_id = c.id
order by 学号 asc;

4.2、右外连接

sql 复制代码
# 查询所有同学的的班级(右外连接)
select s.sn as 学号, s.name as 同学姓名, c.name as 班级名称
from student s
     right 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 s
         right join score on s.id = score.student_id
         right join course c on score.course_id = c.id
order by 学号 asc;

5、自连接

sql 复制代码
# 查询每个同学的班长
select s1.sn as 学号, s1.name as 学生姓名, s2.name 班长姓名
from student s1,
     student s2
where s1.monitor_id = s2.id

6、子查询

sql 复制代码
# 查询名称="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不会去重

sql 复制代码
# 查询名称="xumeng03"的同学和他的班长
select *
from student
where name = "xumeng03"
union
select *
from student
where id = (select monitor_id from student where name = "xumeng03")
相关推荐
山峰哥1 小时前
数据库工程与SQL调优——从索引策略到查询优化的深度实践
数据库·sql·性能优化·编辑器
较劲男子汉1 小时前
CANN Runtime零拷贝传输技术源码实战 彻底打通Host与Device的数据传输壁垒
运维·服务器·数据库·cann
java搬砖工-苤-初心不变1 小时前
MySQL 主从复制配置完全指南:从原理到实践
数据库·mysql
Doro再努力2 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim
Daniel李华2 小时前
echarts使用案例
android·javascript·echarts
WangYaolove13143 小时前
基于python的在线水果销售系统(源码+文档)
python·mysql·django·毕业设计·源码
做人不要太理性3 小时前
CANN Runtime 运行时组件深度解析:任务调度机制、存储管理策略与维测体系构建逻辑
android·运维·魔珐星云
山岚的运维笔记3 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
我命由我123453 小时前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
朗迹 - 张伟4 小时前
Tauri2 导出 Android 详细教程
android