student表
class表
mysql的内连接
# 内连接
# 内连接:合并具有同一列的两个以上的表的行,结果集中不包含一个表与另一个表不匹配的行。
# 简单就是 查询结果只包含他们匹配的行,不匹配的就不要了
select s.stu_name as name,s.stu_age as age,c.class_name as classNam,
c.class_type as classType,c.class_id as classId
from student s,classgrade c
where s.class_id = c.class_id
#外连接
#外连接
# 外连接:合并具有同一列的两个以上的表的行,结果集中除了包含一个表与另一个表匹配的行之外
# 还查询到了左表或右表不匹配的行
# 分为三类
# 左外连接
# 左外连接:两个表在连接过程中除了返回满足连接条件的行之外,还返回左表中不满足条件的行
# 如图左外连接就是左边一整个圆
右外连接:两个表在连接过程中除了返回满足连接条件的行以外,还返回右表中不满足条件的行。如下图中,右外连接就是右边一整个圆。
满外连接:两个表在连接过程中除了返回满足连接条件的行以外,还返回左表和右表中不满足条件的行。如下图中,满外连接就是两个圆所有部分。
左外连接
select s.stu_name as name,c.class_id as classId,s.stu_age as age,
c.class_name as classNam,
c.class_type as classType
# from classgrade c left join student s
from student s left join classgrade c
on s.class_id = c.class_id
# 右外连接就是在 OUTER JOIN 前加一个关键字 RIGHT
# 两个表在连接过程中除了返回满足连接条件的行以外,还返回右表中不满足条件的行。如下图中,右外连接就是右边一整个圆。
七种SQL JOINS的实现
1.根据部门编号 department_id ,查询员工表 employees 中的员工编号 employee_id 和部门表 departments 中其对应的部门名称 department_name 。
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp JOIN departments dept
ON emp.`department_id` = dept.`department_id`;
左外连接
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp LEFT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`;
右外连接
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp RIGHT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`;
第四种JOIN
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp LEFT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`
WHERE dept.`department_id` IS NULL;
第五种JOIN
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`, emp.`department_id`
FROM employees emp RIGHT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`
WHERE emp.`department_id` IS NULL;
满外连接
由于MySQL不支持SQL99语法的满外连接。
4.2 左外连接 和 4.5 第五种JOIN 的并 UNION ALL 即可;或者求4.3 右外连接 和 4.4 第四种JOIN 的并 UNION ALL 也行,都是一样的效果。
# 方法一
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp LEFT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`
UNION ALL
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_id`
FROM employees emp RIGHT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`
WHERE emp.`department_id` IS NULL;
# 方法二
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp RIGHT OUTER JOIN departments dept
ON emp.`employee_id` = dept.`department_id`
UNION ALL
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp LEFT OUTER JOIN departments dept
ON emp.`employee_id` = dept.`department_id`
WHERE dept.`department_id` IS NULL;
第七种JOIN
实现下面这个操作只需要把 4.4 第四种JOIN 和 4.5 第五种JOIN 求 UNION ALL 即可。
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp LEFT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`
WHERE dept.`department_id` IS NULL
UNION ALL
SELECT emp.`employee_id`, emp.`last_name`, dept.`department_name`
FROM employees emp RIGHT OUTER JOIN departments dept
ON emp.`department_id` = dept.`department_id`
WHERE emp.`department_id` IS NULL;