mysql> select e.employee_name,d.department_name from t_employee as e inner join t_department as d on e.department_id = d.id; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | | bb | 财务部 | | cc | 人事部 | +---------------+-----------------+ 3 rows in set (0.00 sec)
mysql> select e.employee_name,d.department_name from t_employee as e join t_department as d on e.department_id = d.id; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | | bb | 财务部 | | cc | 人事部 | +---------------+-----------------+ 3 rows in set (0.00 sec)
mysql> select e.employee_name,d.department_name from t_employee as e,t_department as d where e.department_id = d.id; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | | bb | 财务部 | | cc | 人事部 | +---------------+-----------------+ 3 rows in set (0.00 sec)
mysql> select e.employee_name,d.department_name from t_employee as e inner join t_department as d on e.department_id = d.id and d.department_name = '技术部'; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | +---------------+-----------------+ 1 row in set (0.00 sec)
mysql> select e.employee_name,d.department_name from t_employee as e join t_department as d on e.department_id = d.id where d.department_name = '技术部'; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | +---------------+-----------------+ 1 row in set (0.00 sec)
mysql> select e.employee_name,d.department_name from t_employee as e,t_department as d where e.department_id = d.id and d.department_name = '技术部'; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | +---------------+-----------------+ 1 row in set (0.00 sec)
mysql> select t1.id,t1.employee_name from t_employee as t1, t_employee as t2 where t1.department_id=t2.department_id and t2.employee_name ='aa'; +----+---------------+ | id | employee_name | +----+---------------+ | 1 | aa | | 6 | ff | | 7 | gg | +----+---------------+ 3 rows in set (0.00 sec)
mysql> select e.employee_name,d.department_name from t_employee as e left join t_department as d on e.department_id = d.id where e.department_id !=0; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | | ff | 技术部 | | gg | 技术部 | | bb | 财务部 | | cc | 人事部 | +---------------+-----------------+ 5 rows in set (0.00 sec)
mysql> select e.employee_name,d.department_name from t_employee as e left join t_department as d on e.department_id = d.id where d.department_name is not null; +---------------+-----------------+ | employee_name | department_name | +---------------+-----------------+ | aa | 技术部 | | bb | 财务部 | | cc | 人事部 | | ff | 技术部 | | gg | 技术部 | +---------------+-----------------+ 5 rows in set (0.00 sec)
其实这也就是在外连接的基础上,再使用where条件语句对结果进行过滤而已。
右连接(右外连接)
右连接(右外连接)的语法格式如下所示:
1
select 列 from 从表 right join 主表 on 连接条件;
请注意,无论是左连接还是右连接,on后面紧跟的都是主表中的数据。
例子8:实现和 例子6 一样的需求,即查询所有员工信息及其所在部门,显示员工名称和部门名称:
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql> select d.department_name,e.employee_name from t_department as d right join t_employee as e on e.department_id = d.id; +-----------------+---------------+ | department_name | employee_name | +-----------------+---------------+ | 技术部 | aa | | 技术部 | ff | | 技术部 | gg | | 财务部 | bb | | 人事部 | cc | | NULL | dd | | NULL | ee | +-----------------+---------------+ 7 rows in set (0.00 sec)
mysql> select d.department_name,e.employee_name from t_department as d right join t_employee as e on e.department_id = d.id where e.department_id !=0; +-----------------+---------------+ | department_name | employee_name | +-----------------+---------------+ | 技术部 | aa | | 技术部 | ff | | 技术部 | gg | | 财务部 | bb | | 人事部 | cc | +-----------------+---------------+ 5 rows in set (0.00 sec)
mysql> select d.department_name,e.employee_name from t_department as d left join t_employee as e on e.department_id = d.id -> union -> select d.department_name,e.employee_name from t_department as d right join t_employee as e on e.department_id = d.id; +-----------------+---------------+ | department_name | employee_name | +-----------------+---------------+ | 技术部 | aa | | 技术部 | ff | | 技术部 | gg | | 财务部 | bb | | 人事部 | cc | | NULL | dd | | NULL | ee | | 采购部 | NULL | +-----------------+---------------+ 8 rows in set (0.00 sec)