Summary of seven MySQL JOIN types

Summary of seven MySQL JOIN types

Before we begin, we create two tables to demonstrate the JOIN types we will introduce.

Create a table

CREATE TABLE `tbl_dept` (
   `id` INT(11) NOT NULL AUTO_INCREMENT,
   `deptName` VARCHAR(30) DEFAULT NULL,
   `locAdd` VARCHAR(40) DEFAULT NULL,
   PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_emp` (
   `id` INT(11) NOT NULL AUTO_INCREMENT,
   `name` VARCHAR(20) DEFAULT NULL,
   `deptId` VARCHAR(11) NOT NULL,
   PRIMARY KEY (`id`),
   KEY `fk_dept_id` (`deptId`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Initialize Data

Seven types of JOIN

1. A ∩ B

 SELECT < select_list >
 FROM TableA A
 INNER JOIN TableB B # TOTAL ON A.Key = B.Key 

2. A ( = A ∩ B + A* )

 SELECT < select_list >
 FROM TableA A
 LEFT JOIN TableB B
 ON A.Key = B.Key 

3. B ( = A ∩ B + B* )

 SELECT < select_list >
 FROM TableA A
 RIGHT JOIN TableB B
 ON A.Key = B.Key 

4. A* ( = A - A ∩ B )

 SELECT < select_list >
 FROM TableA A
 LEFT JOIN TableB B
 ON A.Key = B.Key # Main table is retained when ON WHERE B.Key IS NULL # Filter data in table A 

5. B* ( = B - A ∩ B )

 SELECT < select_list >
 FROM TableA A
 RIGHT JOIN TableB B
 ON A.Key = B.Key
 WHERE A.Key IS NULL 

6. A ∪ B

 SELECT < select_list >
 FROM TableA A
 FULL OUTER JOIN TableB B ## FULL OUTER ON A.Key = B.Key is only supported by Oracle 

7. A ∪ B - A ∩ B

 SELECT < select_list >
 FROM TableA A
 FULL OUTER JOIN TableB B
 ON A.Key = B.Key
 WHERE A.Key IS NULL OR B.Key IS NULL 

This is the end of this article about the seven MySQL JOIN types. For more information about MySQL join types, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MYSQL database basics - Join operation principle
  • MySQL join buffer principle
  • Specific usage instructions for mysql-joins
  • Mysql join query syntax and examples
  • Summary of various common join table query examples in MySQL
  • Specific use of MySQL's seven JOINs

<<:  An example of how to implement an adaptive square using CSS

>>:  Implementing timed page refresh or redirect based on meta

Recommend

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

Detailed example of Linux all-round system monitoring tool dstat

All-round system monitoring tool dstat dstat is a...

MySQL date processing function example analysis

This article mainly introduces the example analys...

Vue implements small form validation function

This article example shares the specific code of ...

Solve the problem of VScode configuration remote debugging Linux program

Let's take a look at the problem of VScode re...

Nginx configuration and compatibility with HTTP implementation code analysis

Generate SSL Key and CSR file using OpenSSL To co...

A Deep Dive into JavaScript Promises

Table of contents 1. What is Promise? 2. Why is t...

About the overlap of margin value and vertical margin in CSS

Margin of parallel boxes (overlap of double margi...

A brief discussion on order reconstruction: MySQL sharding

Table of contents 1. Objectives 2. Environmental ...

How to mount a data disk on Tencent Cloud Server Centos

First, check whether the hard disk device has a d...