Table of Contents
Introduction
Home Database Mysql Tutorial How to use JOIN in MySQL

How to use JOIN in MySQL

Jun 03, 2023 am 09:30 AM
mysql join

Introduction

How to use JOIN in MySQL

  • ##A’s unique AB’s public

  • B’s unique AB’s Public

  • AB’s public

  • A’s exclusive

  • B’s exclusive

  • A's unique B's unique AB's public

  • A's unique B's unique

Exercise

Build a table

Department table

DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
	`dept_id` int(11) NOT NULL AUTO_INCREMENT,
    `dept_name` varchar(30) DEFAULT NULL,
    `dept_number` int(11) DEFAULT NULL,
    PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO `dept` VALUES ('1', 'AA', '100');
INSERT INTO `dept` VALUES ('2', 'BB', '200');
INSERT INTO `dept` VALUES ('3', 'CC', '300');
INSERT INTO `dept` VALUES ('4', 'DD', '400');
INSERT INTO `dept` VALUES ('5', 'HH', '500');
Copy after login

Employee table

DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
    `emp_id` int(11) NOT NULL AUTO_INCREMENT,
    `emp_name` varchar(30) DEFAULT NULL,
    `emp_age` int(11) DEFAULT NULL,
    `dept_id` int(11) NOT NULL,
    PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO `emp` VALUES('1', 'zhangsan', '20', '1');
INSERT INTO `emp` VALUES('2', 'lisi', '25', '6');
INSERT INTO `emp` VALUES('3', 'wangwu', '19', '4');
INSERT INTO `emp` VALUES('4', 'zhaoliu', '29', '5');
INSERT INTO `emp` VALUES('5', 'xiaohong', '30', '2');
INSERT INTO `emp` VALUES('6', 'xiaohu', '26', '3');
INSERT INTO `emp` VALUES('7', 'zhangle', '23', '3');
INSERT INTO `emp` VALUES('8', 'qingtian', '38', '3');
INSERT INTO `emp` VALUES('9', 'xiayutian', '36', '2');
INSERT INTO `emp` VALUES('10', 'fangjia', '40', '1');
Copy after login

Scenario analysis

1. Left join

A’s unique AB’s public

SELECT * from emp e LEFT JOIN dept d ON e.dept_id=d.dept_id;
Copy after login

How to use JOIN in MySQL

2. Right join

B’s unique AB’s public

SELECT * from emp e RIGHT JOIN dept d ON e.dept_id=d.dept_id;
Copy after login

How to use JOIN in MySQL

3. Inner join

AB’s public

SELECT * from emp e INNER JOIN dept d ON e.dept_id=d.dept_id;
Copy after login

How to use JOIN in MySQL

4. Left outer join (left join and right table = null)

A’s unique

SELECT * from emp e LEFT JOIN dept d ON e.dept_id=d.dept_id WHERE d.dept_id is null;
Copy after login

How to use JOIN in MySQL

5. Right outer join (right join and left table = null)

B’s unique

SELECT * from emp e RIGHT JOIN dept d ON e.dept_id=d.dept_id WHERE e.dept_id is null;
Copy after login

How to use JOIN in MySQL

6. Full outer join

A’s unique B’s unique AB’s public

Note: MySQL does not support FULL OUTER JOIN (supported in ORACLE).

So use UNION to implement it, you can **merge and remove duplicates**

Application scenario:

Results to be queried It comes from multiple tables, and the multiple tables have no direct connection relationship, but the queried information is consistent

Features:

1. Multiple entries are required

The number of query columns in the query statement is consistent

2. For queries that require multiple query statements

the type and order of each column should be consistent

3 , union keyword **Default deduplication

, if you use union all can contain duplicates**

SELECT * from emp e LEFT JOIN dept d ON e.dept_id=d.dept_id UNION SELECT * FROM emp e RIGHT JOIN dept d ON e.dept_id=d.dept_id;
Copy after login

How to use JOIN in MySQL

7. Full outer join (full outer join and left and right tables = null)

A’s unique B’s unique

SELECT * from emp e LEFT JOIN dept d ON e.dept_id=d.dept_id WHERE d.dept_id is null UNION SELECT * FROM emp e RIGHT JOIN dept d ON e.dept_id=d.dept_id WHERE e.dept_id is null;
Copy after login

How to use JOIN in MySQL

The above is the detailed content of How to use JOIN in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

See all articles