当前位置:网站首页>Usage of JOIN in MySQL
Usage of JOIN in MySQL
2022-07-31 10:36:00 【xiaoweiwei99】
JOIN is used for join table query, there are 5 main usages.The five usages are demonstrated below.
Create 2 tables at will, the structure is as follows
The field col1 is used to make the two tables have a field with the same name (but it is actually useless, because the query conditions need to be specified with ON, here is just to explain that if there is the same field name, it does not matter)
I. Cartesian product: CROSS JOIN
CROSS JOINMake all fields of the two tables directly perform Cartesian product, assuming there are m pieces of data in table 1 and n pieces of data in table 2, the number of results is m*n piecesp>
SELECT * FROM tab1 CROSS JOIN tab2
Results
Second, inner join: INNER JOIN
The inner join needs to use ON to specify the fields to be compared between the two tables, and the final result only displays the data that meets the conditions
SELECT * FROM tab1 INNER JOIN tab2 ON tab1.id1 = tab2.id2
Results
Note that Inner joins only connect data that satisfy the ON condition, which is different from Cartesian product
Third, left join: LEFTJOIN
Left join can be regarded as the basis of inner join, and the data in the left table that does not meet the ON condition is also displayed, but the data in the right table part of the result is NULL
SELECT * FROM tab1 LEFT JOIN tab2 ON tab1.id1 = tab2.id2
Results
Fourth, right join: RIGHTJOIN
Right join is the exact opposite of left join
SELECT * FROM tab1 RIGHT JOIN tab2 ON tab1.id1 = tab2.id2
Results
5. Full connection: OUTERJOIN
Full join is the union of left join and right join, but MySQL does not support full join writing
SELECT * FROM tab1 OUTER JOIN tab2 ON tab1.id1 = tab2.id2
But you can use UNION to combine the result of left join and right join instead
SELECT * FROM tab1 LEFT JOIN tab2 ON tab1.id1 = tab2.id2UNIONSELECT * FROM tab1 RIGHT JOIN tab2 ON tab1.id1 = tab2.id2
Results
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
边栏推荐
- Build finished with errors/Executable Not Found
- 浅谈Attention与Self-Attention,一起感受注意力之美
- Qt compile error: C2228: '.key' must have class/struct/union on the left
- DC-7-vulnhub
- unity-shader-2
- Data Middle Office Construction (6): Data System Construction
- ASP.NET 身份认证框架 Identity(一)
- What does "chmod 777-R filename" mean?
- [Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
- [ verb phrase ] collection
猜你喜欢
怎样使用浏览器静默打印网页
NowCoderTOP17-22 二分查找/排序——持续更新ing
centos7安装mysql5.7
Emotional crisis, my friend's online dating girlfriend wants to break up with him, ask me what to do
csdn file export to pdf
Sql优化总结!详细!(2021最新面试必问)
darknet 硬件软件环境的设置和检测
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
Build finished with errors/Executable Not Found
随机推荐
踩水坑2 数据超出long long
Sql optimization summary!detailed!(Required for the latest interview in 2021)
SQL学习笔记——REGEXP运算符
What does "chmod 777-R filename" mean?
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
逆置问题--重点
学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架
Chapter VII
SQL力扣刷题五
Meikle Studio--Hongmeng 14-day development training notes (8)
单点登录原理及实现方式
NowCoderTOP23-27 Binary tree traversal - continuous update ing
Inversion problem - key point
matlab 读取csv文件绘图
cocoaPods管理之后工程结构变化
项目管理工具之燃尽图:动态考核团队工作能力
业务-(课程-章节-小节)+课程发布一些业务思路
Come n times - 07. Rebuild the binary tree
SQL study notes - REGEXP operator
windows平台下的mysql启动等基本操作