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
When we use the folder properties dialog box in Wi...
Table of contents Preface optimization Extract va...
Table of contents Preface Scenario simulation Sum...
Three modes Bridged (bridge mode), NAT (network a...
Table of contents 1. Introduction to NFS 2. NFS C...
PS: I use PHPStudy2016 here 1. Stop MySQL during ...
【background】 I encountered a very embarrassing th...
This article shares the installation tutorial of ...
I was recently writing a lawyer recommendation we...
Record the installation and configuration method ...
This script can satisfy the operations of startin...
Let’s start the discussion from a common question...
The importance of data consistency and integrity ...
Hello everyone, today when I was looking at the H...
Table of contents Preface Arrow Functions Master ...