当前位置:网站首页>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
边栏推荐
- 单点登录原理及实现方式
- KVM virtualization job
- cocoaPods管理之后工程结构变化
- Experience innovation and iteration through the development of a lucky draw applet
- Simple understanding of GCD
- SQL力扣刷题七
- 湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
- 突破传统可靠性测试:混沌工程优秀实践
- In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
- SQLSERVER将子查询数据合并拼接成一个字段
猜你喜欢
随机推荐
Come n times - 07. Rebuild the binary tree
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
Dart Log工具类
SQL去重的三种方法汇总
Solve rpc error: code = Unimplemented desc = method CheckLicense not implemented
强大的SQL计算利器-SPL
Add a shuffling effect to every pie
Web系统常见安全漏洞介绍及解决方案-CSRF攻击
IBM SPSS Statistics 28软件安装包下载及安装教程
实现弹框组件
Simple understanding of GCD
v-model的原理
unity-shader-2
梅科尔工作室--鸿蒙十四天开发培训笔记(八)
darknet 源码阅读笔记-01-activation_kernels.cu
matlab 读取csv文件绘图
Web系统常见安全漏洞介绍及解决方案-sql注入
Chapter Six
Redis Cluster - Sentinel Mode Principle (Sentinel)
Redis-基础