How to use JOIN in MySQL
Jun 03, 2023 am 09:30 AMIntroduction
- ##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
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');
1. Left join
A’s unique AB’s public
SELECT * from emp e LEFT JOIN dept d ON e.dept_id=d.dept_id;
2. Right join
B’s unique AB’s public
SELECT * from emp e RIGHT JOIN dept d ON e.dept_id=d.dept_id;
3. Inner join
AB’s public
SELECT * from emp e INNER JOIN dept d ON e.dept_id=d.dept_id;
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;
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;
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 consistentFeatures:
1. Multiple entries are requiredThe number of query columns in the query statement is consistent
2. For queries that require multiple query statementsthe 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;
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;
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP?

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

How to fix mysql_native_password not loaded errors on MySQL 8.4

Performance optimization strategies for PHP array paging

How to use MySQL stored procedures in PHP?
