当前位置:网站首页>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
边栏推荐
猜你喜欢

谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!

Linear DP (split)

稀疏数组(非线性结构)

链表(线性结构)

CUDA中的Warp Shuffle

锐捷EBGP 配置案例

Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability

Let every developer use machine learning technology

Problems encountered in uni app development (continuous update)

Ruijie ebgp configuration case
随机推荐
Classic literature reading -- deformable Detr
Current situation analysis of Devops and noops
LeetCode 78. subset
Golang--map扩容机制(含源码)
CUDA中的Warp Shuffle
深入了解JUC并发(二)并发理论
Decryption skills of encrypted compressed files
官方零基础入门 Jetpack Compose 的中文课程来啦!
Community theory | kotlin flow's principle and design philosophy
Summary of WLAN related knowledge points
CUDA与Direct3D 一致性
Sumo tutorial Hello World
CUDA中的函数执行空间说明符
Is there a really free applet?
Style modification of Mui bottom navigation
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
Database learning summary 5
Linear DP (split)
LeetCode 78. 子集
Problems encountered in uni app development (continuous update)