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

CSS border half or partially visible implementation code

1. Use pseudo-classes to display half of the Bord...

Talk about implicit conversion in MySQL

In the course of work, you will encounter many ca...

Full HTML of the upload form with image preview

The upload form with image preview function, the ...

Bug of Chinese input garbled characters in flex program Firefox

Chinese characters cannot be input in lower versio...

Optimize MySQL with 3 simple tweaks

I don't expect to be an expert DBA, but when ...

Solution to slow response of Tomcat server

1. Analytical thinking 1. Eliminate the machine&#...

JavaScript singleton mode to implement custom pop-up box

This article shares the specific code of JavaScri...

Vue: Detailed explanation of memory leaks

What is a memory leak? A memory leak means that a...

A time-consuming troubleshooting process record of a docker error

Table of contents origin Environmental Informatio...

The background color or image inside the div container grows as it grows

Copy code The code is as follows: height:auto !im...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

Introduction to common commands and shortcut keys in Linux

Table of contents 1 System Introduction 2 System ...