又称多表查询。在多个表之间查找符合相应条件的数据。
笛卡尔乘积现象:
表1m行,表2n行,结果=m*n行
发生原因:
没有有效的连接条件
如何避免:
添加连接条件
按年代分类:
1. sql92标准:只支持内连接
2.sql99标准:支持内连接+外连接(左外和右外)+交叉连接
按连接方式分类:
select 查询列表
from 表1 别名,表2 别名
where 表1.key=表2.key
[and 筛选条件]
[group by 分组字段]
[having 分组后筛选]
[order by 排序字段]
1.多表等值连接的结果是多表的交集部分
2.n表连接至少需要n-1个连接条件
3.一般为表起别名
4.多表顺序没有要求
5.可以搭配所有子句使用,分组、筛选等
SELECT NAME,boyName
FROM boys,beauty
WHERE beauty.`boyfriend_id`=boys.`id`;
USE myemployees;
SELECT last_name,department_name
FROM employees,departments
WHERE employees.`department_id`=departments.`department_id`;
SELECT e.last_name,e.`job_id`,job_title
FROM employees e,jobs j
WHERE e.`job_id`=j.`job_id`;
SELECT last_name,department_name
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND e.`commission_pct`IS NOT NULL;
SELECT department_name,city
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
AND city LIKE'_o%';
SELECT COUNT(*) AS 个数,city
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY city;
SELECT d.department_name,d.manager_id,MIN(salary)
FROM departments d,employees e
WHERE d.`department_id`=e.`department_id`
AND commission_pct IS NOT NULL
GROUP BY department_name,d.manager_id;
SELECT COUNT(*)AS 员工个数,job_title
FROM employees e,jobs j
WHERE e.`job_id`=j.`job_id`
GROUP BY job_title
ORDER BY 员工个数;
SELECT last_name,department_name,city
FROM employees e,departments d, locations l
WHERE e.`department_id`=d.`department_id`
AND d.`location_id`=l.`location_id`;
select 查询列表
from 表1 别名,表2 别名
where 非等值连接条件(如某个范围)
[and 筛选条件]
[group by 分组字段]
[having 分组后筛选]
[order by 排序字段]
SELECT salary,grade_level
FROM employees e,job_grades g
WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal`;
select 查询列表
from 表1 别名a,表1 别名b
where 等值连接条件
[and 筛选条件]
[group by 分组字段]
[having 分组后筛选]
[order by 排序字段]
SELECT e.employee_id,e.last_name,m.employee_id,m.`last_name`
FROM employees e,employees m
WHERE e.`manager_id`=m.`employee_id`;
select 查询列表
from 表1 别名 【连接类型】
join 表2 别名
on 连接条件
【where 筛选条件】
【group by 分组】
【having 筛选条件】
【order by 排序列表】
select 查询列表
from 表1 别名 【连接类型】
inner join 表2 别名
on 连接条件
where 筛选条件
SELECT last_name,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`;
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`=j.`job_id`
WHERE e.`last_name`LIKE '%e%';
SELECT city ,COUNT(*)AS 部门个数
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING 部门个数>3;
SELECT COUNT(*)AS 个数,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
HAVING 个数>3
ORDER BY 个数 DESC;
SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
INNER JOIN jobs j
ON e.`job_id`=j.`job_id`
ORDER BY department_name DESC;
SELECT salary,grade_level
FROM employees e
JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal`AND g.`highest_sal`;
SELECT COUNT(*),grade_level
FROM employees e
JOIN job_grades g
ON e.`salary`BETWEEN g.`lowest_sal`AND g.`highest_sal`
GROUP BY grade_level
HAVING COUNT(*)>20
ORDER BY grade_level DESC;
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id`=m.`employee_id`;
SELECT d.*,e.employee_id
FROM departments d
LEFT OUTER JOIN employees e
ON d.`department_id`=e.`department_id`
WHERE e.`employee_id` IS NULL;
SELECT d.*,e.employee_id
FROM employees e
RIGHT OUTER JOIN departments d
ON d.`department_id`=e.`department_id`
WHERE e.`employee_id` IS NULL;
SELECT b.*,bo.*
FROM beauty b
CROSS JOIN boys bo;