当前位置:网站首页>SQL - Left join, Right join, Inner join
SQL - Left join, Right join, Inner join
2022-07-31 10:37:00 【xiaoweiwei99】
Article Directory
Foreword
I have been doing SQL-related exercises recently, and I found that the SQL I used to be so confident has become tricky if I don't touch it for a long time, especially the internal and external connections between this table.So this article is about sorting out this internal and external connection.
I. Concept
First of all, let's introduce the definitions of these three
1.Left join: that is, left join, which is based on the left table, and connects the two tables according to the conditions of the two tables given after ON.As a result, all the query information in the left table will be listed, while the right table will only list the parts that satisfy the condition after ON and the left table.Left join is called left outer join, which is a type of outer join.
2.Right join: The right join is based on the right table, and connects the two tables according to the conditions of the two tables given after ON.As a result, all the query information in the right table will be listed, while the left table will only list the parts that satisfy the condition after ON and the right table.Right join is called right outer join, which is a kind of outer join.
3.Inner join: that is, inner join, using the two tables as reference objects at the same time, and connecting the two tables according to the conditions of the two tables given after ON.As a result, only the parts of the two tables that satisfy the ON condition will be listed.
There is no full outer join in MySQL, so it is not explained here.
2. Examples
Table table_a
table table_b
----------------------------------------------------------------Dividing line-----------------------------------------------------------------
Left join: left join
SQL Statement:
select *from table_a left join table_b on table_a.id=table_b.id;
Results:
Right join: right join
SQL Statement:
select * from table_a right join table_b on table_a.id=table_b.id;
Result:
Inner join: Inner join
SQL Statement:
select * from table_a inner join table_b on table_a.id=table_b.id;
Result:
Summary
Based on the chestnuts above, it looks much clearer and more contrasting.If you still think that you only understand it now, but you still find it a little difficult to use, you can use the following collective thinking, which I have only recently discovered, and it will be much simpler to think about it this way.
We have two tables: A and B, then treat them as a set respectively
Then our left and right outer joins and inner joins are equivalent to this ↓↓↓
A Left join B-> A (according to the return requirement, it needs to connect the part of B with A and other conditions)
A Right join B -> B (according to the return requirements, the parts of A and B need to be connected)
A Inner join B -> A∩B (the part that needs to be connected with conditions such as A and B according to the return requirement)
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 【23提前批】北森云计算-测开面经
- Burndown chart of project management tools: Dynamic assessment of team work ability
- NowCoderTOP28-34 binary tree - continuous update ing
- Come n times - 07. Rebuild the binary tree
- 双链表的创建
- [Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
- 【LeetCode】387. 字符串中的第一个唯一字符
- Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1
- ASP.NET 身份认证框架 Identity(一)
- 解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
猜你喜欢
Sql优化总结!详细!(2021最新面试必问)
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
Redis-基础
Open Kylin openKylin automation developer platform officially released
单点登录原理及实现方式
【LeetCode】21. 合并两个有序链表
unity-shader-2
SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
模块八
DC-7-vulnhub
随机推荐
IBM SPSS Statistics 28软件安装包下载及安装教程
Centos7 install mysql5.7
强大的SQL计算利器-SPL
【LeetCode】387. 字符串中的第一个唯一字符
KVM虚拟化作业
The fifth chapter
Inversion problem - key point
一种用于保证多方子系统数据一致性的方法
cocoaPods管理之后工程结构变化
MySQL中JOIN的用法
透过开发抽奖小程序,体会创新与迭代
NowCoderTOP28-34 binary tree - continuous update ing
windows平台下的mysql启动等基本操作
Come n times - 07. Rebuild the binary tree
Implement a thread pool
Simple understanding of GCD
Android安全专题(三)JNI混淆
Data Middle Office Construction (6): Data System Construction
Module eight
Dart Log工具类