时间:2021-09-16 09:54:08 | 栏目:Mysql | 点击:次
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, '设计部'); insert into t_dept(id, name) values(2, '开发部'); insert into t_dept(id, name) values(3, '测试部'); insert into t_emp(id, name, deptId) values(1, '张三', 1); insert into t_emp(id, name, deptId) values(2, '李四', 2); insert into t_emp(id, name, deptId) values(3, '王五', 0); # ps:为了说明方便,t_emp 表 说成 A 表, t_dept 表说成 B 表
目录
SELECT * FROM t_emp e INNER JOIN t_dept d ON e.deptId = d.id;
SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id;
SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId = d.id;
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;
SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId= d.id WHERE d.id is null;
SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId= d.id WHERE e.id is null;
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;