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

Introduction to the use of the indeterminate property of the checkbox

When we use the folder properties dialog box in Wi...

An example of elegantly writing status labels in Vue background

Table of contents Preface optimization Extract va...

Reasons and solutions for MySQL sql_mode modification not taking effect

Table of contents Preface Scenario simulation Sum...

Analysis of the implementation process of three modes of VMWare network adapter

Three modes Bridged (bridge mode), NAT (network a...

Change the MySQL database engine to InnoDB

PS: I use PHPStudy2016 here 1. Stop MySQL during ...

Solution for forgetting the root password of MySQL5.7 under Windows 8.1

【background】 I encountered a very embarrassing th...

MySQL 8.0.24 installation and configuration method graphic tutorial

This article shares the installation tutorial of ...

HTML code to add icons to transparent input box

I was recently writing a lawyer recommendation we...

MySQL 8.0.12 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Shell script nginx automation script

This script can satisfy the operations of startin...

CSS method of controlling element height from bottom to top and from top to bottom

Let’s start the discussion from a common question...

How does MySQL ensure data integrity?

The importance of data consistency and integrity ...

JavaScript implements changing the color of a web page through a slider

Hello everyone, today when I was looking at the H...

Summary of several common ways to abbreviate javascript code

Table of contents Preface Arrow Functions Master ...