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;
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!