MySQL inner join, left join, right join, outer join, multi-table queryBuild Environment:create table t_emp( id int primary key, name varchar(20), deptId int ); create table t_dept( id int primary key, name varchar(20) ); insert into t_dept(id, name) values(1, 'Design Department'); insert into t_dept(id, name) values(2, 'Development Department'); insert into t_dept(id, name) values(3, 'Test Department'); insert into t_emp(id, name, deptId) values(1, '张三', 1); insert into t_emp(id, name, deptId) values(2, 'Li Si', 2); insert into t_emp(id, name, deptId) values(3, '王五', 0); # ps: For the sake of convenience, the t_emp table is referred to as table A and the t_dept table is referred to as table B Table of contents 1. INNER JOIN (A ∩ B)SELECT * FROM t_emp e INNER JOIN t_dept d ON e.deptId = d.id; 2. LEFT JOIN Left outer join (A all)SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id; 3. RIGHT JOIN Right Outer Join (B All)SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId = d.id; 4. FULL JOIN Full Outer Join (A + B)SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id UNION SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId = d.id; 5. LEFT Excluding JOIN (A - B, i.e. unique to table A) +SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId= d.id WHERE d.id is null; 6. RIGHT Excluding JOIN (B - A, i.e. B table only)SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId= d.id WHERE e.id is null; 7. OUTER Excluding JOIN (A and B are unique to each other)SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id WHERE d.id is null UNION SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId= d.id WHERE e.id is null; SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Example code for implementing the wavy water ball effect using CSS
Basic knowledge of responsive layout development ...
This article uses examples to illustrate the func...
1. Virtual environment follows the project, creat...
This article describes how to use Docker's mu...
Table of contents definition grammar Examples 1. ...
To understand what this means, we must first know ...
<div class="box"> <img /> &...
How to create a Linux virtual machine in VMware a...
It is very simple to install Scala environment in...
This is my first blog. It’s about when I started ...
This tutorial shares the detailed steps of instal...
Function: Jump to the previous page or the next p...
What is an index? An index is a data structure th...
Table of contents 1. What is a window function? 1...
ps: Here is how to disable remote login of root a...