当前位置:网站首页>SQL三种连接:内连接、外连接、交叉连接
SQL三种连接:内连接、外连接、交叉连接
2022-07-05 06:15:00 【Ostrich5yw】
SQL三种连接:内连接、外连接、交叉连接
准备两张表并以PersonId相关联。

一、交叉连接(CROSS JOIN)
交叉连接返回被连接的两个表所有数据行的笛卡尔积。
// 这两句sql完全等价
select * from person CROSS JOIN address
select * from person, address
查询结果:
二、内连接(INNER JOIN)
内连接可以获取两表的公共部分的记录,即利用条件表达式来消除交叉连接的某些数据行。
// 这两句sql完全等价
select * from person INNER JOIN address ON person.PersonId = address.PersonId
select * from person, address WHERE person.PersonId = address.PersonId
查询结果:注意此处PersonId = 4的小孙并未输出
所以内连接只查询两表中都包合的数据
三、外连接(LEFT JOIN、RIGHT JOIN、FULL JOIN)
注意:外连接时,应注意where与on的区别
- on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
- where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉
1.左外连接
左向外联接的结果集包括指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)
select * from person LEFT JOIN address ON person.PersonId = address.PersonId
查询结果:注意此处PersonId = 4的‘小孙’也输出并且由于‘小孙’不存在对应的address,所以address的数据置为null
2.右外连接
右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值(null)
select * from person RIGHT JOIN address ON person.PersonId = address.PersonId
查询结果:注意此处AddressId = 6的‘广东’也输出并且由于‘广东’不存在对应的person,所以preson的数据置为null
3.完整外连接
完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值(null)Mysql中不支持FULL JOIN,所以这里用UNION联合查询代替
select * from person LEFT JOIN address ON person.PersonId = address.PersonId
UNION
select * from person RIGHT JOIN address ON person.PersonId = address.PersonId
查询结果:同时输出了'小孙'以及'广东'
边栏推荐
- Is it impossible for lamda to wake up?
- Data visualization chart summary (I)
- Daily question 1984 Minimum difference in student scores
- MySQL advanced part 2: MySQL architecture
- The difference between CPU core and logical processor
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
- redis发布订阅命令行实现
- CPU内核和逻辑处理器的区别
- [rust notes] 15 string and text (Part 1)
- 可变电阻器概述——结构、工作和不同应用
猜你喜欢
[practical skills] technical management of managers with non-technical background
Smart construction site "hydropower energy consumption online monitoring system"
SPI details
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Appium自动化测试基础 — Appium测试环境搭建总结
LaMDA 不可能觉醒吗?
How to adjust bugs in general projects ----- take you through the whole process by hand
1.13 - RISC/CISC
Sqlmap tutorial (1)
Groupbykey() and reducebykey() and combinebykey() in spark
随机推荐
Usage scenarios of golang context
Appium基础 — 使用Appium的第一个Demo
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
One question per day 1447 Simplest fraction
QQ computer version cancels escape character input expression
[rust notes] 14 set (Part 2)
【Rust 笔记】13-迭代器(中)
SQLMAP使用教程(二)实战技巧一
[rust notes] 17 concurrent (Part 1)
Some common problems in the assessment of network engineers: WLAN, BGP, switch
[practical skills] how to do a good job in technical training?
LVS简介【暂未完成(半成品)】
Daily question 1342 Number of operations to change the number to 0
R language [import and export of dataset]
Quickly use Amazon memorydb and build your own redis memory database
leetcode-31:下一个排列
Overview of variable resistors - structure, operation and different applications
Full Permutation Code (recursive writing)
数据可视化图表总结(一)
Leetcode divide and conquer / dichotomy