image.png
内连接用于查询两个表都有
外连接用于查询一个表有,另一个表没有
/*
内连接 inner
外连接
左外 left [outer]
右外 right
全外 full
交叉连接 cross
select 查询列表
from 表1 别名 [连接类型]
join 表2 别名
on [连接条件]
[where 筛选条件]
[group by 分组]
[having 筛选条件]
[order by 排序列表]
*/
USE myemployees;
/*
内连接 交集
select 查询列表
from 表1 别名
inner join 表2 别名
on [连接条件]
[where 筛选条件]
[group by 分组]
[having 筛选条件]
[order by 排序列表]
分类:
等值
可以添加筛选 分组 排序
inner 可以省略
筛选条件放在where后面,连接条件放在on后面,提高分离性,便于阅读
inner join和sql等值连接效果一致,都是查询多表的交集
非等值
自连接
*/
#查询员工名和对应的部门名
SELECT last_name,department_name
FROM departments
INNER JOIN employees
ON departments.`department_id` = employees.`department_id`;
#查询名字中包含e的员工名和工种名(添加筛选)
SELECT e.`last_name`,j.`job_title`
FROM employees e
INNER JOIN jobs j
ON e.`job_id` = j.`job_id`
WHERE e.`last_name` LIKE '%e%';
#查询部门个数>3的城市名和部门个数(添加分组+筛选)
#1查询每个城市部门个数
#查询1基础上满足筛选条件的结果
SELECT l.`city`,COUNT(*)c
FROM departments d
INNER JOIN locations l
ON d.`location_id` = l.`location_id`
GROUP BY l.`city`
HAVING c>3;
#查询部门的部门员工个数大于3的部门名称和员工个数,并按个数降序
SELECT d.`department_name`,COUNT(*) c
FROM employees e
INNER JOIN departments d
ON e.`department_id` = d.`department_id`
GROUP BY d.`department_id`
HAVING c>3
ORDER BY c DESC;
#查询员工名 部门名 工种名 并按部门名称降序
SELECT e.`last_name`,d.`department_name`,j.`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 d.`department_name` DESC;
# 非等值连接
# 查询员工的工资级别
SELECT e.`salary`,j.`grade_level`
FROM employees e
INNER JOIN job_grades j
ON e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal`;
#查询每个工资级别的个数>2,并按工资级别降序
SELECT j.`grade_level` AS g,COUNT(j.`grade_level`) AS c
FROM employees e
INNER JOIN job_grades j
ON e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal`
GROUP BY g
HAVING c>2
ORDER BY g DESC;
#自连接
#查询姓名中包含字符k员工的名字,上级的名字
SELECT e.`last_name`,m.`last_name`
FROM employees AS e
INNER JOIN employees AS m
ON e.`manager_id` = m.`employee_id`
WHERE e.`last_name` LIKE '%k%';
#二 外连接
/*
应用场景:
用于查询一个表中有,另一个表中没有的记录
特点:
1. 外连接的查询结果为主表中的所有记录
如果从表中有和它匹配的,则显示匹配的值
如果从表中没有和它匹配的,则显示null
外连接查询结果=内连接的查询结果+主表中有而从表中没有的记录
2. 左外连接: left join左边的是主表
右外连接: right join右边的是主表
3. 左外和右外交换两个表的顺序,可以实现同样的效果
4. 全外连接 = 内连接的结果+表1中有但表2没有+表2中有但表1中没有
*/
# 查询男朋友不在男神表的女神名
SELECT beauty.id AS gid, beauty.`name` AS gname, boys.`id` AS bid, boys.`boyName` AS bname
FROM beauty
LEFT OUTER JOIN boys
ON beauty.`boyfriend_id` = boys.`id`
WHERE boys.`id` IS NOT NULL;
SELECT beauty.`name`,boys.*
FROM beauty
LEFT OUTER JOIN boys
ON beauty.`boyfriend_id` = boys.`id`
WHERE boys.`id` IS NOT NULL;
# 查询员工表中没有经理的员工
SELECT *
FROM employees e
LEFT OUTER JOIN departments d
ON e.`manager_id` = d.`department_id`;
SELECT *
FROM departments d
LEFT OUTER JOIN employees e
ON e.`department_id` = d.`department_id`;
#查询哪个部门没有员工
SELECT d.`department_id`, e.`employee_id`
FROM departments d
LEFT OUTER JOIN employees e
ON e.`department_id` = d.`department_id`
WHERE e.employee_id IS NOT NULL;
SELECT d.`department_id`, e.`employee_id`
FROM employees e
RIGHT OUTER JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE e.employee_id IS NOT NULL;
#全外 mysql不支持
SELECT *
FROM departments d
FULL OUTER JOIN locations l
ON d.`location_id` = l.`location_id`
#交叉连接结果 即为 笛卡尔乘积
SELECT *
FROM employees
CROSS JOIN departments;
# 查询编号>3的女神的男朋友信息,如果有则列出详细,如果没有,则用null填充
SELECT g.id,g.name,b.*
FROM beauty g
LEFT OUTER JOIN boys b
ON g.boyid = b.id
WHERE g.id>3;
#查询哪个城市没有部门
SELECT l.*,d.`department_id`
FROM locations l
LEFT JOIN departments d
ON l.`location_id` = d.`location_id`
WHERE d.`department_id` IS NULL;
# 查询部门名为SAT 或 IT 的员工信息
SELECT e.`employee_id`,e.`last_name`,d.`department_name`
FROM employees e
RIGHT OUTER JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE d.`department_name` IN('SAL','IT');
```
网友评论