MySQL学习笔记------多表查询

目录

多表关系

一对多

多对多

一对一

多表查询

概述

分类

内连接(交集)

隐式内连接

显式内连接

​编辑

外连接(from后为左表,join后为右表)

左外连接

右外连接

自连接

[联合查询(union,union all)](#联合查询(union,union all))

子查询

分类

标量子查询

列子查询

行子查询

表子查询


多表关系

概述

项目开发中,在进行数据库表结构设计时,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在各种联系

基本分为三类:一对多,多对多,一对一

一对多

典型案例:部门与员工

实现:在多的一方建立一个外键,指向一的一方的主键

多对多

典型案例:学生与课程的关系

实现:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键

sql 复制代码
#建立学生表
create table student(
    id int auto_increment primary key  comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
)comment '学生表';
insert into student(id, name, no) VALUES (1,'孔明','1000000000'),(2,'士元','1000000001'),(3,'仲达','1000000003'),(4,',孟德','1000000004');
#建立课程表
create table course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
)comment '课程表';
insert into course(id, name) VALUES (1,'Java'),(2,'MySQL'),(3,'PHP'),(4,'C++');
#建立学生课程表
create table student_course(
    id int auto_increment primary key comment '主键',
    student_id int not null comment '学生ID',
    course_id int not null comment '课程编码',
    constraint fk_courseid foreign key (course_id)references  course(id),
    constraint fk_student_id foreign key (student_id)references student(id)
)comment '学生课程中间表';
insert into student_course(id, student_id, course_id) VALUES (1,1,1),(2,2,2),(3,3,3),(4,4,4);

添加外键后出现蓝色小钥匙

一对一

案例:用户与用户详情

关系:一对一,用于单表拆分,将表的基础字段放在一张表中,其他详情字段放到另一张表中,提高操作效率

实现:在任意一方加入外键,关联另一方的主键,并且设置外键为唯一的(unique)

多表查询

概述

建表

sql 复制代码
create table dept(
    id int auto_increment comment 'ID' primary key ,
    name varchar(50) not null comment '部门名称'
)comment '部门表';
insert into dept(id,name) VALUES (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经办'),(6,'人事部');
create table employee(
    #唯一标识,主键,自动增长为auto_increment
    id int primary key auto_increment comment 'ID',
    #不为空且唯一
    name varchar(10) not null unique comment '姓名',
    #大于零小于120
    age int check ( age>0&&age<120 )comment '年龄',
    job varchar(20) comment '职务',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    dept_id int comment '部门id'
)comment '员工表';
insert into employee(id,name,age,job,salary,entrydate,dept_id)values (1,'关羽',22,'开发',3000,'2018-11-1',1),
                                                                     (2,'刘备',34,'经理',10000,'2011-2-11',2),
                                                                     (3,'张飞',18,'程序员鼓励师',8000,'2020-1-2',4),
                                                                     (4,'曹操',30,'董事长',2000000,'1990-3-4',5),
                                                                     (5,'周瑜',16,'财务总监',10000,'2000-3-5',3);

查询:

sql 复制代码
#查询---笛卡尔积
select *from employee,dept;

此时查询,会匹配每个人所有部门匹配一次

修改之后

sql 复制代码
select *from employee,dept where employee.dept_id=dept.id;

分类

例如:AB两表

连接查询

内连接:相当于查询A、B交集部分数据

外连接:

左外连接:查询左表所有数据,以及两张表交集部分数据

右外连接:查询右表所有数据,以及两边交集部分数据

自连接:当前表与自身的连接查询,自连接必须使用表别名

子查询

内连接(交集)

内连接分为隐式和显式

隐式内连接

select 字段列表 from 表1,表2 where 条件...;

以上面部门表和员工表为例

sql 复制代码
#查询员工及其关联部门名称
select employee.name,dept.name from employee,dept where employee.dept_id=dept.id;

显式内连接

select 字段列表 from 表1 [inner] join 表2 on 连接条件...;

sql 复制代码
#显式
select employee.name,dept.name from employee inner join dept on employee.dept_id=dept.id;

外连接(from后为左表,join后为右表)

左外连接

select 字段列表 from 表1 left [outer] join 表2 on 条件...;

相当于查询表1(左表)的所有数据(包含表交集部分)

sql 复制代码
#左外连接
select e.*,d.name from employee e left outer join dept d on e.dept_id=d.id;

完全包含左表

右外连接

select 字段列表 from 表1 right [outer] join 表2 on 条件...;

相当于查询表2(右表)的所有数据(包含表交集部分)

sql 复制代码
#右外连接
select d.*,e.* from employee e right outer join dept d on e.dept_id=d.id;

自连接

自连接查询语法(必须取别名)

select 字段列表 from 表A 别名A jion 表A 别名B on 条件...;(join可省略)

自连接查询,可以是内连接查询,也可以是外连接查询。

sql 复制代码
#自连接
select a.name,b.name from employee a,employee b where a.manageid=b.id;

给表添加了上司id

联合查询(union,union all)

对于union查询,就是把多次查询的结果合并,形成一个新的查询结果集

语法:

select 字段列表 from 表A ...

union[all]

select 字段列表 from 表B......;

sql 复制代码
#联合查询
select * from employee where age>20
union all
select *from employee where salary>20000;

去掉all可去重

注意事项:对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致

子查询

SQL语句中嵌套select语句,称为嵌套查询,又称子查询

语法:

select * from t1 where column1=(select co;lumn1 from t2);

子查询外部的语句可以是insert/update/delect/select的任何一个

分类

根据子查询结果不同可分为

标量子查询(子查询结果为单个值)

列子查询(子查询结果为1列)

行子查询(子查询结果为1行)

表子查询(子查询结果为多行多列)

根据子查询位置分为:where之后、from之后、select之后。

标量子查询

子查询返回的结果是单个值(数字、日期、字符串等),最简单的形式

sql 复制代码
#标量子查询
select*from employee where dept_id=(select id from dept where name='财务部');
#()内返回值为销售部id

列子查询

sql 复制代码
#列子查询
select *from employee where dept_id in (select id from dept where name='财务部'or name='研发部');
#空格内返回一列

行子查询

sql 复制代码
#行子查询
select *from employee where (salary,manageid)=(select salary,manageid from employee where name='周瑜');

表子查询

sql 复制代码
#表子查询
select e.*,d.*from (select*from employee where  entrydate>'2000-01-01') e left outer join dept d on e.dept_id=d.id;
相关推荐
锅巴编程5 分钟前
【芋道源码】gitee很火的开源项目pig——后台管理快速开发框架使用笔记(微服务版之本地开发环境篇)
笔记·gitee·开源
南山忆8742 小时前
Docker入门指南:快速学习Docker的基本操作
学习·docker·微服务
jsnjs3 小时前
MapReduce学习与理解
大数据·学习·mapreduce
地球空间-技术小鱼3 小时前
学习单片机编程和硬件设计步骤
单片机·嵌入式硬件·学习
speop3 小时前
【笔记】选择题笔记+数据结构笔记
数据结构·笔记
木鬼与槐3 小时前
MySQL高阶2004-职员招聘人数
数据库·mysql
吃什么芹菜卷3 小时前
深度学习:卷积神经网络CNN
人工智能·笔记·深度学习·cnn
蓝瑟柳絮3 小时前
学习之什么是生成器
android·java·学习
乐事layz3 小时前
对比学习训练是如何进行的
深度学习·学习·机器学习