Detailed explanation of four types of MySQL connections and multi-table queries

Detailed explanation of four types of MySQL connections and multi-table queries

MySQL inner join, left join, right join, outer join, multi-table query

Build Environment:

create table t_emp(
	id int primary key, 
	name varchar(20),
	deptId int
);
create table t_dept(
	id int primary key,
	name varchar(20)
);
insert into t_dept(id, name) values(1, 'Design Department');
insert into t_dept(id, name) values(2, 'Development Department');
insert into t_dept(id, name) values(3, 'Test Department');
insert into t_emp(id, name, deptId) values(1, '张三', 1);
insert into t_emp(id, name, deptId) values(2, 'Li Si', 2);
insert into t_emp(id, name, deptId) values(3, '王五', 0);
# ps: For the sake of convenience, the t_emp table is referred to as table A and the t_dept table is referred to as table B

Table of contents

1. INNER JOIN (A ∩ B)

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-0cqsICkR-1619189927045)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805175111307.png)]

SELECT * FROM t_emp e INNER JOIN t_dept d ON e.deptId = d.id; 

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-YVymu987-1619189927047)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805175234304.png)]

2. LEFT JOIN Left outer join (A all)

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-860jyRX1-1619189927049)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805175441802.png)]

SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id; 

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-PXjhiYYa-1619189927051)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805175712415.png)]

3. RIGHT JOIN Right Outer Join (B All)

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-s0OFZKj1-1619189927054) (9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805175813118.png)]

SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId = d.id; 

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-bx74QMwd-1619189927056)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805175959230.png)]

4. FULL JOIN Full Outer Join (A + B)

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-b125K8dF-1619189927057) (9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805180116571.png)]

SELECT * FROM t_emp e LEFT JOIN t_dept d 
ON e.deptId = d.id UNION 
SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId = d.id; 

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-qLSli8R9-1619189927058) (9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805180416613.png)]

5. LEFT Excluding JOIN (A - B, i.e. unique to table A) +

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-NQFvqcNu-1619189927060)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805180611367.png)]

SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId= d.id WHERE d.id is null; 

insert image description here

6. RIGHT Excluding JOIN (B - A, i.e. B table only)

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-0Ak37mwF-1619189927062)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805181033398.png)]

SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId= d.id WHERE e.id is null; 

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-7czqoOP5-1619189927062)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805181145897.png)]

7. OUTER Excluding JOIN (A and B are unique to each other)

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-9YEEUSOD-1619189927063)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/20190805181255259.png)]

SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id WHERE d.id is null
UNION
SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId= d.id WHERE e.id is null; 

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-dzO9X4QC-1619189927064)(9mysql_multi-table query_inner join_right join_left join_nested query.assets/2019080518143030.png)]

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • mysql subquery and join table details
  • Comparison of efficiency between single-table query and multi-table join query in MySql database
  • Problems with join queries and subqueries in MySQL
  • Detailed explanation of MySQL multi-table join query
  • MySQL connection query from the basics to the advanced

<<:  Example code for implementing the wavy water ball effect using CSS

>>:  Analysis of the issues and solutions for repeated submission, repeated refresh, and backoff prevention

Recommend

Responsive layout summary (recommended)

Basic knowledge of responsive layout development ...

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the func...

Create a virtual environment using venv in python3 in Ubuntu

1. Virtual environment follows the project, creat...

How to reduce image size using Docker multi-stage build

This article describes how to use Docker's mu...

JS uses the reduce() method to process tree structure data

Table of contents definition grammar Examples 1. ...

Getting Started Tutorial for Beginners ④: How to bind subdirectories

To understand what this means, we must first know ...

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

MySQL 5.7.18 free installation version window configuration method

This is my first blog. It’s about when I started ...

Linux installation MySQL tutorial (binary distribution)

This tutorial shares the detailed steps of instal...

Interaction in web design: A brief discussion on paging issues

Function: Jump to the previous page or the next p...

Various types of MySQL indexes

What is an index? An index is a data structure th...

Specific use of MySQL window functions

Table of contents 1. What is a window function? 1...

Linux system prohibits remote login command of root account

ps: Here is how to disable remote login of root a...