当前位置:网站首页>Learn about various joins in SQL and their differences
Learn about various joins in SQL and their differences
2022-07-02 06:21:00 【Jiang Xia】
Author's other platforms :
| CSDN:blog.csdn.net/qq_41153943
| Nuggets :juejin.cn/user/651387…
| You know :www.zhihu.com/people/1024…
| GitHub:github.com/JiangXia-10…
This article altogether 2215 word , Expected reading 610 minute
Preface
We're writing sql At the time of statement , The most painful is the joint query involving multiple tables , Especially various links of various tables , what inner join、left join、right join、union、union all, You may feel confused when you see this .
Today's article is about studying and sorting out , These various join What exactly do they mean , What's the difference between .
Text
First, suppose we have two tables .Table user It's the watch on the left .Table t_user It's the watch on the right . Each has a certain record , There are two records username It's the same , As shown below : Let's see the difference JOIN What is the difference in the use of :
user surface :

t_user surface :

1.INNER JOIN
First look at it. inner join, This is also used more , Let's execute the following statement , Use the left table and the right table inner join Joint query :
SELECT * FROM user INNER JOIN t_user ON user.username = t_user.username
The result data of the query is as follows :

It can be found that the result of the query is the same intersection of the associated fields of the left table and the right table . That is, the query result is the data of the overlapped part in the figure below .

2.LEFT JOIN
left join It is also one of the more common joint query methods used in daily development . Again, let's look at the examples first and then discuss the results , Execute the following statement :
SELECT * FROM user LEFT JOIN t_user ON user.username = t_user.username
The results of the query are as follows :

At this time, if you change the left table to the right table, what is the result ?
SELECT * FROM t_user LEFT JOIN user ON user.username = t_user.username

At this time, you can summarize and use left join The result of the query is based on the left table , Query all the data in the left table , The data in the right table only shows the matching data , The mismatched data is null. Here is left .username= Right .username, So the data in the right table shown here is in the right table username And in the left table username Equal data , The right table of unequal parts is empty . It can be shown in the figure below , The result of the query is the shaded part of the red mark , That is, all the data in the left table and the data in the overlapping part of the right table and the left table .

3.RIGHT JOIN
Actually right join and left join It's like , The result of the query is the opposite .
Execute the following statement :
SELECT * FROM user RIGHT JOIN t_user ON user.username = t_user.username

That is, the query results here are based on the data in the right table , All the data in the right table are displayed , The left table shows the data of the intersection with the right table . It can be shown in the figure below , All values in the right table of the query results , If there is a match in the left table, there is a value , If it doesn't match null Express , and left join contrary !

4.UNION And UNION ALL
UNION The operator is used to merge two or more SELECT The result set of the statement . however ,UNION Inside SELECT Statements must have the same number of columns . Columns must also have similar data types . meanwhile , Every one of them SELECT The order of the columns in the statement must be the same .UNION Select only records , and UNION ALL All records will be listed .
First look at union:
SELECT username FROM user UNION SELECT username FROM t_user
The query results are as follows :

Look again union all:
SELECT username FROM user UNION all SELECT username FROM t_user
The query results are as follows :

You can find union and union all comparison ,union Two pieces of data are missing , Namely user Table and t_user Table duplicate data , therefore union The selected records will be filtered , Delete duplicate data , and union all Then all data will be selected .
union It can be expressed as follows :

union all Shown by the following :

5.CROSS JOIN
cross join Cross connect , Also known as Cartesian connection (cartersian join) Or cross multiplication (product), If A and B It's two sets , Their cross connections are marked as :A*B, For example, execute the following statement :
SELECT * FROM user a CROSS JOIN t_user b
Some of the results are as follows :

You can see that the resulting result set is the product of the respective numbers of the two tables , Usually, this kind of operation should be avoided in actual operation .
summary
That's all sql Some join queries in the statement and the differences between them , There's another one full join Indicates full connection , however mysql China does not support it. , It can be represented by the combination of left connection and right connection .full join It represents the union of left table and right table , But for records that do not match , Will take null Express .
Finally, there is a picture on the Internet that can well show the relationship between these connections , Here's the picture :

Related to recommend
边栏推荐
- LeetCode 39. Combined sum
- Generic classes and parameterized classes of SystemVerilog
- 亚马逊aws数据湖工作之坑1
- CUDA用户对象
- Golang--map扩容机制(含源码)
- WLAN相关知识点总结
- 深入学习JVM底层(二):HotSpot虚拟机对象
- LeetCode 90. 子集 II
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
- Deep learning classification network -- alexnet
猜你喜欢

社区说|Kotlin Flow 的原理与设计哲学
![Data science [viii]: SVD (I)](/img/cb/7bf066a656d49666985a865c3a1456.png)
Data science [viii]: SVD (I)

Ros2 --- lifecycle node summary

最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)

Google Play Academy 组队 PK 赛,正式开赛!

IPv6 experiment and summary

Data playback partner rviz+plotjuggler

深入学习JVM底层(三):垃圾回收器与内存分配策略

Zhuanzhuanben - LAN construction - Notes

Common means of modeling: combination
随机推荐
Lucene Basics
Support new and old imperial CMS collection and warehousing tutorials
复杂 json数据 js前台解析 详细步骤《案例:一》
BGP中的状态机
一起学习SQL中各种join以及它们的区别
深入学习JVM底层(二):HotSpot虚拟机对象
浏览器原理思维导图
Ros2 --- lifecycle node summary
栈(线性结构)
LeetCode 283. 移动零
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
Current situation analysis of Devops and noops
CUDA中的Warp Shuffle
Talking about MySQL database
I/o impressions from readers | prize collection winners list
Detailed notes of ES6
Deep learning classification network -- vggnet
TensorRT中的循环
BGP routing optimization rules and notification principles
Data science [viii]: SVD (I)