Home  >  Article  >  Database  >  How to use JOIN in MySQL

How to use JOIN in MySQL

WBOY
WBOYforward
2023-06-03 09:30:351493browse

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');

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');

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;

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;

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;

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;

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;

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;

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;

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:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete