多表连接分类:内连接、外连接、交叉连接
多表连接方式:
- 内连接:inner join,join
- 外连接:left join,left outer join,right join,right outer join,union
- 交叉连接:cross join
通过具体实例进行分析:
department部门表:
employee员工表:
其中,外键对应关系为:employee.dept_id=department.id。employee.leader中的数字的含义为:张一的leader是张二,张二没有leader,张三的leader是张一。
1. 内连接
inner join 或 join(等同于inner join)
语法:
SELECT ··· FROM table_1
[INNER] JOIN table_2
[ON join_condition]
WHERE where_definition
解释:只列出这些连接表中与连接条件相匹配的数据行。INNER可以不写,则默认为内连接。[ON join_condition]里面写的是连接的条件。
举例:
select e.name, d.name from employee e inner join department d on e.dept_id = d.id;
或
select e.name, d.name from employee e join department d on e.dept_id = d.id;
等价于:
select e.name,d.name from employee e , department d where e.dept_id = d.id;
运行结果: