当前位置:网站首页>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 '
边栏推荐
- 2021apmcm post game Summary - edge detection
- Real time clock (RTC)
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- Sqlmap tutorial (II) practical skills I
- Daily question 1189 Maximum number of "balloons"
- Is it impossible for lamda to wake up?
- 【LeetCode】Easy | 20. Valid parentheses
- 【Rust 笔记】15-字符串与文本(下)
- 1041 Be Unique
- Filter the numbers and pick out even numbers from several numbers
猜你喜欢
LaMDA 不可能觉醒吗?
Quickly use Amazon memorydb and build your own redis memory database
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
leetcode-6111:螺旋矩阵 IV
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
Leetcode-6110: number of incremental paths in the grid graph
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Appium自动化测试基础 — Appium测试环境搭建总结
MIT-6874-Deep Learning in the Life Sciences Week 7
Leetcode-6108: decrypt messages
随机推荐
leetcode-9:回文数
可变电阻器概述——结构、工作和不同应用
高斯消元 AcWing 884. 高斯消元解异或线性方程组
SPI 详解
Chapter 6 relational database theory
Leetcode-9: palindromes
Sqlmap tutorial (1)
1040 Longest Symmetric String
博弈论 AcWing 894. 拆分-Nim游戏
Leetcode-31: next spread
【Rust 笔记】16-输入与输出(下)
MySQL advanced part 1: index
博弈论 AcWing 891. Nim游戏
【LeetCode】Easy | 20. Valid parentheses
【Rust 笔记】17-并发(上)
A reason that is easy to be ignored when the printer is offline
How to generate an image from text on fly at runtime
栈 AcWing 3302. 表达式求值
对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively