MySQL多表查询
多表查询
多表查询,也称为关联查询,指两个或更多个表一起完成查询操作。
前提条件:这些一起查询的表之间是有关系的(一对一、一对多),它们之间一定是有关联字段,这个关联字段可能建立了外键,也可能没有建立外键。比如:员工表和部门表,这两个表依靠“部门编号”进行关联。
错误的方式:
SELECT employee_id,department_name
FROM employees,departments;
这种错误被称为笛卡尔积的错误,笛卡尔积的错误会在下面条件下产生:
- 省略多个表的连接条件(或关联条件)
- 连接条件(或关联条件)无效
- 所有表中的所有行互相连接
为了避免笛卡尔积, 可以在 WHERE 加入有效的连接条件。
正确的多表查询方式:
SELECT employee_id,department_name
FROM employees,departments
WHERE employees.department_id = departments.department_id;
不能查询到department_id为NULL的。
当我们想查询department_id时,可能会出现错误:
SELECT employee_id,department_name,department_id
FROM employees,departments
WHERE employees.department_id = departments.department_id;# 报错
Column 'department_id' in field list is ambiguous,这是因为我们两个表中都有department_id。sql不知道是哪一个,所以我们应该指明:
SELECT employee_id,department_name,employees.department_id
FROM employees,departments
WHERE employees.department_id = departments.department_id;
建议:从sql优化的角度,建议多表查询时,每个字段前都指明其所在的表。
SELECT employees.employee_id,departments.department_name,employees.department_id
FROM employees,departments
WHERE employees.department_id = departments.department_id;
我们注意到,当列名多的时候,我们的语句会非常长。
我们可以给表起别名,在SELECT和WHERE中使用表的别名。
SELECT emp.employee_id,dept.department_name,emp.department_id
FROM employees AS emp,departments AS dept
WHERE emp.department_id = dept.department_id;
注意:如果给表起了别名,一旦在SELECT和WHERE中使用表名,则必须使用表的别名,而不能使用表的原名(因为先执行的是FROM,别名会覆盖掉表名)
练习:查询员工的employee_id,last_name,department_name,city
SELECT
e.employee_id,
e.last_name,
d.department_id,
d.department_name,
l.city
FROM
employees AS e,
departments AS d,
locations AS l
WHERE
e.department_id = d.department_id
AND d.location_id = l.location_id;
结论:如果有n个表实现多表的查询,则需要至少n-1个连接条件。
多表查询的分类
- 等值连接与非等值连接
- 自连接与非自连接
- 内连接与外连接
等值连接与非等值连接
非等值连接的例子:
SELECT
e.employee_id,
e.last_name,
e.salary,
j.grade_level
FROM
employees AS e,
job_grades AS j
WHERE
e.salary BETWEEN j.lowest_sal AND j.highest_sal;
自连接与非自连接
练习:查询员工id,员工姓名以及其管理者的id和姓名
SELECT
emp.employee_id,
emp.first_name,
emp.last_name,
mgr.department_id,
mgr.first_name,
mgr.last_name
FROM
employees AS emp,
employees AS mgr
WHERE
emp.manager_id = mgr.employee_id;
内连接与外连接
- 内连接: 合并具有同一列的两个以上的表的行, 结果集中不包含一个表与另一个表不匹配的行。
外连接: 两个表在连接过程中除了返回满足连接条件的行以外还返回左(或右)表中不满足条件的行(条件运算结果为0或者NULL,通常为NULL),这种连接称为左(或右) 外连接。没有匹配的行时, 结果表中相应的列为空(NULL)。
- 如果是左外连接,则连接条件中左边的表也称为 主表 ,右边的表称为 从表 。
- 如果是右外连接,则连接条件中右边的表也称为 主表 ,左边的表称为 从表 。
内连接
之前的例子:
SELECT last_name,department_name
FROM employees e ,departments d
WHERE e.department_id = d.department_id;
实现外连接
SQL92使用+
来连接,但是MySQL不支持这种写法实现外连接。
所以我们将目光放到SQL99。
SQL99语法中实现外连接
SQL99语法中使用JOIN ...ON
的方式实现多表的查询,这种方式也能解决外连接的问题。MySQL支持此种方式的写法。
那么SQL99语法如何实现多表查询呢?
SQL99语法实现内连接:
SELECT last_name,department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id;
三张表的情况:
SELECT
e.employee_id,
e.last_name,
d.department_id,
d.department_name,
l.city
FROM employees AS e
JOIN departments AS d ON e.department_id = d.department_id
JOIN locations AS l ON d.location_id = l.location_id;
总结,使用JOIN...ON子句创建连接的语法结构:
SELECT table1.column, table2.column,table3.column
FROM table1
JOIN table2 ON table1 和 table2 的连接条件
JOIN table3 ON table2 和 table3 的连接条件
它的嵌套逻辑类似我们使用的 FOR 循环。
语法说明:
- 可以使用 ON 子句指定额外的连接条件。
- 这个连接条件是与其它条件分开的。
- ON 子句使语句具有更高的易读性。
- 关键字 JOIN、INNER JOIN、CROSS JOIN 的含义是一样的,都表示内连接
左外连接:
SELECT last_name,department_name
FROM employees AS e LEFT OUTER JOIN departments AS d
ON e.department_id = d.department_id;
其中:OUTER可省略
右外连接:
SELECT last_name,department_name
FROM employees AS e RIGHT JOIN departments AS d
ON e.department_id = d.department_id;
UNION的使用
UNION和UNION ALL
UNION 操作符返回两个查询的结果集的并集,去除重复记录。
UNION ALL操作符返回两个查询的结果集的并集。对于两个结果集的重复部分,不去重。
注意:执行UNION ALL语句时所需要的资源比UNION语句少。如果明确知道合并数据后的结果数据不存在重复数据,或者不需要去除重复的数据,则尽量使用UNION ALL语句,以提高数据查询的效率。
7种SQL JOIN的实现
中图:内连接
SELECT last_name,department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id;
左上图:左外连接
SELECT last_name,department_name
FROM employees AS e LEFT JOIN departments AS d
ON e.department_id = d.department_id;
右上图:右外连接
SELECT last_name,department_name
FROM employees AS e RIGHT JOIN departments AS d
ON e.department_id = d.department_id;
左中图:
SELECT last_name,department_name
FROM employees AS e LEFT JOIN departments AS d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;
右中图:
SELECT last_name,department_name
FROM employees AS e RIGHT JOIN departments AS d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;
左下图(满外连接):
方式1:左上图 UNION ALL 右中图
SELECT last_name,department_name
FROM employees AS e LEFT JOIN departments AS d
ON e.department_id = d.department_id
UNION ALL
SELECT last_name,department_name
FROM employees AS e RIGHT JOIN departments AS d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;
方式2:左中图 UNION ALL 右上图
SELECT last_name,department_name
FROM employees AS e LEFT JOIN departments AS d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL
UNION
SELECT last_name,department_name
FROM employees AS e RIGHT JOIN departments AS d
ON e.department_id = d.department_id;
右下图:
左中图 UNION ALL 右中图
SELECT last_name,department_name
FROM employees AS e LEFT JOIN departments AS d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL
UNION ALL
SELECT last_name,department_name
FROM employees AS e RIGHT JOIN departments AS d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;
SQL99语法的新特性1:自然连接
SQL99 在 SQL92 的基础上提供了一些特殊语法,比如 NATURAL JOIN
用来表示自然连接。我们可把
自然连接理解为 SQL92 中的等值连接。它会帮你自动查询两张连接表中 所有相同的字段 ,然后进行等值连接 。
在SQL92标准中:
SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
ON e.`department_id` = d.`department_id`
AND e.`manager_id` = d.`manager_id`;
在SQL99中可以这样写:
SELECT employee_id,last_name,department_name
FROM employees e NATURAL JOIN departments d;
或者使用USING连接
SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
USING(department_id,manager_id);
当我们进行连接的时候,SQL99还支持使用 USING 指定数据表里的 同名字段 进行等值连接。但是只能配合JOIN一起使用。
推荐使用ON
注意:我们要 控制连接表的数量 。多表连接就相当于嵌套 for 循环一样,非常消耗资源,会让 SQL 查询性能下降得很严重,因此不要连接不必要的表。在许多 DBMS 中,也都会有最大连接表的限制。
【强制】超过三个表禁止 join。需要 join 的字段,数据类型保持绝对一致;多表关联查询时, 保证被关联的字段需要有索引。
练习
1.显示所有员工的姓名,部门号和部门名称。
SELECT e.first_name,e.last_name,d.department_id,d.department_name
FROM employees AS e LEFT JOIN departments AS d
ON e.department_id = d.department_id;
2.查询90号部门员工的job_id和90号部门的location_id
SELECT e.job_id,d.location_id
FROM employees AS e JOIN departments AS d
ON e.department_id = d.department_id
WHERE e.department_id = 90;
3.选择所有有奖金的员工的 last_name , department_name , location_id , city
SELECT last_name, d.department_name, l.location_id, city
FROM employees AS e
LEFT JOIN departments AS d
ON e.department_id = d.department_id
LEFT JOIN locations AS l
ON d.location_id = l.location_id
WHERE commission_pct IS NOT NULL;
在这里,需要写两个left,因为需要“对齐”
4.选择city在Toronto工作的员工的 last_name , job_id , department_id , department_name
SELECT last_name, e.department_id, department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
LEFT JOIN locations l
ON d.location_id = l.location_id
WHERE l.city = 'Toronto';
5.查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为’Executive’
SELECT department_name, street_address, last_name, job_title, salary
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
LEFT JOIN locations l
ON d.location_id = l.location_id
LEFT JOIN jobs j
ON e.job_id = j.job_id
WHERE d.department_name = 'Executive';
6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
employees Emp# manager Mgr#
kochhar 101 king 100
SELECT e.last_name 'employees', e.employee_id 'Emp#', m.last_name 'mannager', m.employee_id 'Mgr#'
FROM employees AS e
LEFT JOIN employees AS m
ON e.manager_id = m.employee_id
7.查询哪些部门没有员工
SELECT d.department_id
FROM employees AS e
RIGHT JOIN departments AS d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;
8.查询哪个城市没有部门
SELECT city,department_id
FROM departments d
RIGHT JOIN locations l
ON d.location_id = l.location_id
WHERE department_id IS NULL;
- 查询部门名为 Sales 或 IT 的员工信息
SELECT employee_id, last_name, department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_name IN ('Sales','IT');