当前位置:网站首页>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 77. 组合
- In depth understanding of JUC concurrency (II) concurrency theory
- 【程序员的自我修养]—找工作反思篇二
- 500. Keyboard line
- VLAN experiment of switching technology
- LeetCode 90. Subset II
- BGP中的状态机
- 步骤详解 | 助您轻松提交 Google Play 数据安全表单
- Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
- 官方零基础入门 Jetpack Compose 的中文课程来啦!
猜你喜欢
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
VLAN experiment of switching technology
Monitoring uplink of VRRP
深入学习JVM底层(四):类文件结构
The official zero foundation introduction jetpack compose Chinese course is coming!
Don't use the new WP collection. Don't use WordPress collection without update
数据科学【九】:SVD(二)
让每一位开发者皆可使用机器学习技术
In depth understanding of JUC concurrency (I) what is JUC
Browser principle mind map
随机推荐
Shenji Bailian 3.52-prim
LeetCode 77. combination
数据科学【八】:SVD(一)
LeetCode 283. 移动零
Jetpack Compose 与 Material You 常见问题解答
谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
The official zero foundation introduction jetpack compose Chinese course is coming!
Detailed notes of ES6
来自读者们的 I/O 观后感|有奖征集获奖名单
CUDA中内置的Vector类型和变量
Singleton mode compilation
【张三学C语言之】—深入理解数据存储
Web components series (VIII) -- custom component style settings
Ros2 --- lifecycle node summary
BGP 路由優選規則和通告原則
TensorRT中的循环
BGP报文详细解释
Eco express micro engine system has supported one click deployment to cloud hosting
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
深入学习JVM底层(二):HotSpot虚拟机对象