当前位置:网站首页>Mysql_12 多表查询
Mysql_12 多表查询
2022-08-05 00:01:00 【萨科塔资深干员】
多表查询
一对一,一对多,多对多(两个一对多)
一对一:其中一方设置unique的外键。
一对多:多的一方设置外键。
多对多:建立中间表,设置两个外键。
直接查询:select * from A, B;
笛卡尔积,结果是AB两表所有记录的排列组合。后面再加where可以筛选查询条件。
内连接
只连接两个表共有部分。
select * from A, B where A.id=B.id;-- 隐式内连接
select * from A [inner] join B on 条件;
外连接
完全包含左/右表中的数据。另一个表没有对应数据则补null。
select * from A left [outer] join B on 条件;-- 左外连接
select * from A right [outer] join B on 条件;-- 右外连接
自连接
select * from A AS1 inner join A AS2 where 条件;-- 自连接可以是内或外连接。必须起别名!!!
联合 Union
Union 会把查询结果联合起来。
union all:结果可能重复。
union:重复的结果合并。
想做union操作,两个要联合的表必须字段数相同,字段类型匹配。
子查询
上一次查询的结果作为下一次查询的条件。
select * from (select * from b);
标量子查询:结果为一个。可以做> < =操作。
列子查询:结果为一列。可以做 IN NOT IN SOME ALL ANY 操作。(ANY SOME 等价)
行子查询:结果为一行。可以做 = <> IN NOT IN 操作。
表子查询:结果为多行多列。最常用 IN NOT IN 操作。
边栏推荐
猜你喜欢
SQL关联表更新
找不到DiscoveryClient类型的Bean
The master teaches you the 3D real-time character production process, the game modeling process sharing
First, the basic concept of reptiles
NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path
Security software Avast and Symantec NortonLifeLock merge with UK approval, market value soars 43%
KT148A语音芯片怎么烧录语音进入芯片里面通过串口和电脑端的工具
Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
STC89C52RC的P4口的应用问题
Couple Holding Hands [Greedy & Abstract]
随机推荐
jenkins send mail system configuration
2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)
.net(C#)获取两个日期间隔的年月日
Ab3d.PowerToys and Ab3d.DXEngine Crack
what is MVCC
KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions
基于Appian低代码平台开发一个SpaceX网站
情侣牵手[贪心 & 抽象]
SQL association table update
10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻
STC89C52RC的P4口的应用问题
【CVA估值训练营】财务建模指南——第一讲
【手撕AHB-APB Bridge】~ AMBA总线 之 AHB
leetcode经典例题——单词拆分
游戏3D建模入门,有哪些建模软件可以选择?
The role of the annotation @ EnableAutoConfiguration and how to use
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
【SSR服务端渲染+CSR客户端渲染+post请求+get请求+总结】
Laravel 实现redis分布式锁
【七夕情人节特效】-- canvas实现满屏爱心