/*
使用union实现全连接的效果
(1)A ∪ B
select 字段列表
from A表 left join B表
on 关联条件
union
select 字段列表
from A表 right join B表
on 关联条件
(3)A ∪ B - A ∩ B
select 字段列表
from A表 left join B表
on 关联条件
where 从表的关联字段 is null
union
select 字段列表
from A表 right join B表
on 关联条件
where 从表的关联字段 is null
*/
#查询所有员工和部门信息,包括那些没有部门的员工和没有员工的部门
SELECT *
FROM t_employee LEFT JOIN t_department
ON t_employee.did = t_department.did
UNION
SELECT *
FROM t_employee RIGHT JOIN t_department
ON t_employee.did = t_department.did
#查询那些没有部门的员工和没有员工的部门
/*
where xxx is null
xxx 看从表
员工表和部门表来说,员工表是从表。
因为主表的字段,例如did是不可能为null
*/
SELECT *
FROM t_employee LEFT JOIN t_department
ON t_employee.did = t_department.did
WHERE t_employee.did IS NULL
UNION
SELECT *
FROM t_employee RIGHT JOIN t_department
ON t_employee.did = t_department.did
WHERE t_employee.did IS NULL