当前位置:网站首页>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 tab2Results

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.id2Results

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.id2Results

Fourth, right join: RIGHTJOIN
Right join is the exact opposite of left join
SELECT * FROM tab1 RIGHT JOIN tab2 ON tab1.id1 = tab2.id2Results

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.id2But 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.id2Results

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
边栏推荐
- Windows安装mysql详细步骤(通俗易懂,简单上手)
- 逆置问题--重点
- (C language) program environment and preprocessing
- Redis缓存面临的缓存穿透问题
- Sql优化总结!详细!(2021最新面试必问)
- 我们能做出来数据库吗?
- Windows系统Mysql8版本的安装教程
- web安全入门-黑苹果MAC系统安装
- Day113. Shangyitong: user authentication, Alibaba Cloud OSS, patient management
- Detailed explanation of SQL stored procedures
猜你喜欢

Data Middle Office Construction (6): Data System Construction

新人学习小熊派华为iot介绍

SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)

初识二叉搜索树

Burndown chart of project management tools: Dynamic assessment of team work ability

Redis Cluster - Sentinel Mode Principle (Sentinel)

Chapter VII

Single sign-on principle and implementation

双链表的插入和删除

Rich text editor Tinymce
随机推荐
Redis缓存面临的缓存雪崩问题
踩水坑2 数据超出long long
透过开发抽奖小程序,体会创新与迭代
Come n times with the sword--05. Replace spaces
ASP.NET 身份认证框架 Identity(一)
Burndown chart of project management tools: Dynamic assessment of team work ability
odoo14 | 附件上传功能及实际使用
[ 动词词组 ] 合集
Mybaits 常用问题详解
(C language) program environment and preprocessing
【LeetCode】21. 合并两个有序链表
Centos7 install mysql5.7
强大的SQL计算利器-SPL
Echart饼图添加轮播效果
Qt compile error: C2228: '.key' must have class/struct/union on the left
Insertion and deletion of doubly linked list
乐观锁和悲观锁
Data Middle Office Construction (6): Data System Construction
Use turtle to draw buttons
【23提前批】北森云计算-测开面经