当前位置:网站首页>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 操作。
边栏推荐
- 【LeetCode】图解 904. 水果成篮
- Couple Holding Hands [Greedy & Abstract]
- 【云原生--Kubernetes】Pod控制器
- Three tips for you to successfully get started with 3D modeling
- Uniapp dynamic sliding navigation effect demo (finishing)
- 话题 | 雾计算和边缘计算有什么区别?
- The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
- Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account
- 【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
- 子连接中的参数传递
猜你喜欢

Getting started with 3D modeling for games, what modeling software can I choose?

4 - "PyTorch Deep Learning Practice" - Backpropagation

【LeetCode】矩阵模拟相关题目汇总

子连接中的参数传递

Nuclei (2) Advanced - In-depth understanding of workflows, Matchers and Extractors

从单体架构迁移到 CQRS 后,我觉得 DDD 并不可怕

leetcode经典例题——单词拆分

Day118. Shangyitong: order list, details, payment

SQL association table update

统计单词(DAY 101)华中科技大学考研机试题
随机推荐
Basic web in PLSQL
线程三连鞭之“线程的状态”
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
【LeetCode】滑动窗口题解汇总
安全软件 Avast 与赛门铁克诺顿 NortonLifeLock 合并案获英国批准,市值暴涨 43%
MVCC是什么
KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions
2022年华数杯数学建模
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
What is next-generation modeling (with learning materials)
头脑风暴:完全背包
【数据挖掘概论】数据挖掘的简单描述
.net(C#)获取两个日期间隔的年月日
数据类型及输入输出初探(C语言)
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
LeetCode Hot 100
Metasploit-域名上线隐藏IP
网站最终产品页使用单一入口还是多入口?
注解@EnableAutoConfiguration的作用以及如何使用
【LeetCode】Summary of Two Pointer Problems