当前位置:网站首页>There are three kinds of SQL connections: internal connection, external connection and cross connection
There are three kinds of SQL connections: internal connection, external connection and cross connection
2022-07-05 06:18:00 【Ostrich5yw】
SQL Three connections : Internal connection 、 External connection 、 Cross connect
Prepare two sheets and PersonId Related to .

One 、 Cross connect (CROSS JOIN)
Cross connect returns the Cartesian product of all data rows of the connected two tables .
// These two sentences sql Completely equivalent
select * from person CROSS JOIN address
select * from person, address
Query results :
Two 、 Internal connection (INNER JOIN)
The internal connection can obtain the records of the common part of the two tables , That is to use conditional expressions to eliminate some data rows of cross connection .
// These two sentences sql Completely equivalent
select * from person INNER JOIN address ON person.PersonId = address.PersonId
select * from person, address WHERE person.PersonId = address.PersonId
Query results : Notice here PersonId = 4 My little grandson didn't export
Therefore, the inner connection only queries the data contained in both tables 
3、 ... and 、 External connection (LEFT JOIN、RIGHT JOIN、FULL JOIN)
Be careful : When connecting outside , Attention should be paid to where And on The difference between
- on The condition is the condition used to generate the temporary table , It doesn't care on Is the condition in true , Will return to the records in the table on the left .
- where The condition is that after the temporary table is generated , Then filter the temporary table . There is no such thing as left join The meaning of ( You have to go back to the records in the table on the left ) 了 , If the conditions are not true, filter them out
1. The left outer join
The result set of the left out join includes all rows of the specified left table , Not just the rows that the join columns match . If a row in the left table does not match a row in the right table , All selection list columns in the right table in the associated result set row are null (null)
select * from person LEFT JOIN address ON person.PersonId = address.PersonId
Query results : Notice here PersonId = 4 Of ‘ Little grandson ’ Also output and due to ‘ Little grandson ’ There is no corresponding address, therefore address Data set to null
2. Right connection
The right outer join is the reverse join of the left outer join . All rows of the right table will be returned . If a row in the right table does not match a row in the left table , Null value will be returned for the left table (null)
select * from person RIGHT JOIN address ON person.PersonId = address.PersonId
Query results : Notice here AddressId = 6 Of ‘ guangdong ’ Also output and due to ‘ guangdong ’ There is no corresponding person, therefore preson Data set to null
3. Complete external connection
Full outer join returns all rows in the left and right tables . When a row has no matching row in another table , The selection list column of another table contains null values (null)Mysql China does not support it. FULL JOIN, So here we use UNION Joint query instead of
select * from person LEFT JOIN address ON person.PersonId = address.PersonId
UNION
select * from person RIGHT JOIN address ON person.PersonId = address.PersonId
Query results : At the same time, it outputs ' Little grandson ' as well as ' guangdong '
边栏推荐
猜你喜欢

Appium自动化测试基础 — Appium测试环境搭建总结

Matrixdb V4.5.0 was launched with a new mars2 storage engine!

栈 AcWing 3302. 表达式求值

Leetcode-6110: number of incremental paths in the grid graph

传统数据库逐渐“难适应”,云原生数据库脱颖而出

快速使用Amazon MemoryDB并构建你专属的Redis内存数据库

Traditional databases are gradually "difficult to adapt", and cloud native databases stand out

SPI details

MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!

Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
随机推荐
leetcode-9:回文数
RGB LED infinite mirror controlled by Arduino
背包问题 AcWing 9. 分组背包问题
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
leetcode-6110:网格图中递增路径的数目
[leetcode] day95 effective Sudoku & matrix zeroing
4. Object mapping Mapster
QQ电脑版取消转义符输入表情
1040 Longest Symmetric String
传统数据库逐渐“难适应”,云原生数据库脱颖而出
MySQL advanced part 1: triggers
Golang uses context gracefully
11-gorm-v2-02-create data
Flutter Web 硬件键盘监听
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
SPI 详解
2021apmcm post game Summary - edge detection
MySQL advanced part 1: stored procedures and functions
[rust notes] 17 concurrent (Part 1)
1.13 - RISC/CISC