当前位置:网站首页>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
边栏推荐
- Reading classic literature -- Suma++
- Ros2 --- lifecycle node summary
- Detailed steps of JS foreground parsing of complex JSON data "case: I"
- In depth understanding of JUC concurrency (II) concurrency theory
- Let every developer use machine learning technology
- 步骤详解 | 助您轻松提交 Google Play 数据安全表单
- CUDA中内置的Vector类型和变量
- 日期时间API详解
- Frequently asked questions about jetpack compose and material you
- LeetCode 83. Delete duplicate elements in the sorting linked list
猜你喜欢

Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk

Web components series (VIII) -- custom component style settings

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

Deep learning classification network -- vggnet

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

Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken

Community theory | kotlin flow's principle and design philosophy

穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!

【张三学C语言之】—深入理解数据存储

Google Play Academy 组队 PK 赛,正式开赛!
随机推荐
深入学习JVM底层(四):类文件结构
CUDA中内置的Vector类型和变量
CUDA中的线程层次
Deep learning classification network -- alexnet
递归(迷宫问题、8皇后问题)
Cglib代理-代码增强测试
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
TensorRT的命令行程序
LeetCode 283. Move zero
让每一位开发者皆可使用机器学习技术
BGP报文详细解释
LeetCode 90. Subset II
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
LeetCode 40. 组合总和 II
From design delivery to development, easy and efficient!
从设计交付到开发,轻松畅快高效率!
LeetCode 39. 组合总和
CUDA中的Warp matrix functions
BGP 路由优选规则和通告原则
浅谈三点建议为所有已经毕业和终将毕业的同学