MySQL多表查询(重要)
一、连表查询
- 连接:将有联系的多张表通过关联(有联系就行,不一定是外键)字段,进行连接,形参一张大表
- 连表查询:在大表的基础上进行查询,就称之为连表查询
- 将表与表建立连接的方式有四种:内连接、左连接、右连接、全连接
数据准备
mysql>:
create table dep(
id int primary key auto_increment,
name varchar(16),
work varchar(16)
);
create table emp(
id int primary key auto_increment,
name varchar(16),
salary float,
dep_id int
);
insert into dep values(1, '市场部', '销售'), (2, '教学部', '授课'), (3, '管理部', '开车');
insert into emp(name, salary, dep_id) values('egon', 3.0, 2),('yanghuhu', 2.0, 2),('sanjiang', 10.0, 1),('owen', 88888.0, 2),('liujie', 8.0, 1),('yingjie', 1.2, 0);
笛卡尔积(基本没用)
# 笛卡尔积: 集合 X{a, b} * Y{o, p, q} => Z{{a, o}, {a, p}, {a, q}, {b, o}, {b, p}, {b, q}}
mysql>: select * from emp, dep;
# 总结:是两张表 记录的所有排列组合,数据没有利用价值
内连接
- 关键字:
inner join on(inner可以省略)
- 语法:
from A表 inner join B表 on A表.关联字段=B表.关联字段
mysql>:
select emp.id,emp.name,salary,dep.name,work
from dep inner join emp on dep.id=emp.dep_id; # 内连接的inner可以省略
总结:只保留两个表有关联的数据
左连接
- 关键字:
left join on
- 语法:
from A表 left join B表 on A表.关联字段=B表.关联字段
mysql>:
select
emp.id,emp.name,salary,dep.name,work
from emp left join dep on emp.dep_id = dep.id
order by emp.id;
总结:保留左表的全部数据,右表有对应数据直接连表显示,没有对应关系空填充
右连接
- 关键字:
right join on
- 语法:
from A表 left join B表 on A表.关联字段=B表.关联字段
mysql>:
select
emp.id,emp.name,salary,dep.name,work
from emp right join dep on emp.dep_id = dep.id
order by emp.id;
总结:保留右表的全部数据,左表有对应数据直接连表显示,没有对应关系空填充
左右连接可以相互转化
mysql>:
select
emp.id,emp.name,salary,dep.name,work
from emp right join dep on emp.dep_id = dep.id
order by emp.id;
mysql>:
select
emp.id,emp.name,salary,dep.name,work
from dep left join emp on emp.dep_id = dep.id
order by emp.id;
总结:更换一下左右表的位置,相对应更换左右连接关键字,结果相同
全连接
将左连接和右连接通过关键字相连即实现全连接
关键字:union
mysql>:
select
emp.id,emp.name,salary,dep.name,work
from emp left join dep on emp.dep_id = dep.id
union
select
emp.id,emp.name,salary,dep.name,work
from emp right join dep on emp.dep_id = dep.id
order by id;
总结:左表右表数据都被保留,彼此有对应关系正常显示,彼此没有对应关系均空填充对方
一对一与一对多情况一致
create table author(
id int,
name varchar(64),
detail_id int
);
create table author_detail(
id int,
phone varchar(11)
);
insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 2);
insert into author_detail values(1, '13344556677'), (2, '14466779988'), (3, '12344332255');
select author.id,name,phone from author join author_detail on author.detail_id = author_detail.id order by author.id;
select author.id,name,phone from author left join author_detail on author.detail_id = author_detail.id
union
select author.id,name,phone from author right join author_detail on author.detail_id = author_detail.id
order by id;
多对多
create table author(
id int,
name varchar(64),
detail_id int
);
insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 0);
create table book(
id int,
name varchar(64),
price decimal(5,2)
);
insert into book values(1, 'python', 3.66), (2, 'Linux', 2.66), (3, 'Go', 4.66);
create table author_book(
id int,
author_id int,
book_id int
);
# 数据:author-book:1-1,2 2-2,3 3-1,3
insert into author_book values(1,1,1),(2,1,2),(3,2,2),(4,2,3),(5,3,1),(6,3,3);
# 多对多
select book.name, book.price, author.name from book
join author_book on book.id = author_book.book_id
join author on author_book.author_id = author.id;
# 多对多对1
select book.name, book.price, author.name, author_detail.phone from book
join author_book on book.id = author_book.book_id
join author on author_book.author_id = author.id
left join author_detail on author.detail_id = author_detail.id;
二、子查询(重点***)
子查询:将一条查询sql的结果作为另一条sql的条件
- 增:insert into 表 select子查询结果
- 删:delete from 表 where条件 select子查询(查询的表不能与delete表相同)
- 查:select 字段 from 表 where条件 select子查询
- 改:update 表 set 字段=值 where select子查询(查询的表不能与update表相同)
# 子查询的sql
select dep, max(salary) from emp group by dep;
# 将子查询转换为一张表
create table t1(dep_name varchar(64), max_salary decimal(5,2));
## 子查询 - 增
insert into t1 select dep, max(salary) from emp group by dep;
## 子查询 - 删(查询的表不能与delete表相同)
delete from t1 where dep_name in (select distinct dep from emp);
## 子查询 - 改(查询的表不能与delete表相同)
update t1 set max_salary=max_salary+1 where dep_name in (select distinct dep from emp);
## 子查询 - 查
select * from emp where (dep, salary) in (select dep, max(salary) from emp group by dep);
三、all与any:区间修饰条件
# 语法规则
where id in (1, 2, 3) # id是1或2或3
where id not in (1, 2, 3) # id不是1,2,3
where salary < all(3, 6, 9) # salary必须小于所有情况(小于最小)
where salary > all(3, 6, 9) # salary必须大于所有情况(大于最大)
where salary < any(3, 6, 9) # salary只要小于一种情况(小于最大)
where salary > any(3, 6, 9) # salary只要大于一种情况(大于最小)
# 案例,查询员工表中 薪资低于id大于11的员工薪资 所有的员工信息
select * from emp where salary < all(select salary from emp where id>11);
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现