当前位置:网站首页>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
边栏推荐
猜你喜欢
Deep learning classification network -- Network in network
Google play academy team PK competition, official start!
The official zero foundation introduction jetpack compose Chinese course is coming!
Frequently asked questions about jetpack compose and material you
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
Lucene Basics
Community theory | kotlin flow's principle and design philosophy
【张三学C语言之】—深入理解数据存储
社区说|Kotlin Flow 的原理与设计哲学
从设计交付到开发,轻松畅快高效率!
随机推荐
Use some common functions of hbuilderx
Arduino Wire 库使用
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Redis---1. Data structure characteristics and operation
BGP报文详细解释
It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
CUDA中的Warp Shuffle
复杂 json数据 js前台解析 详细步骤《案例:一》
Bgp Routing preference Rules and notice Principles
【张三学C语言之】—深入理解数据存储
BGP routing optimization rules and notification principles
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
Eco express micro engine system has supported one click deployment to cloud hosting
深入了解JUC并发(一)什么是JUC
Singleton mode compilation
深入学习JVM底层(五):类加载机制
【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
Reading classic literature -- Suma++
Detailed explanation of BGP message