分类:
– 内连接 [inner] join on
– 外连接
• 左外连接 left [outer] join on
• 右外连接 right [outer] join on
含义:又称多表查询
使用 ON 子句创建多表连接

  1. SELECT employee_id, city, department_name
  2. FROM employees e
  3. JOIN departments d
  4. ON d.department_id = e.department_id
  5. JOIN locations l
  6. ON d.location_id = l.location_id;

image.png

join连接

  1. SELECT
  2. bt.id,
  3. NAME,
  4. boyName
  5. FROM
  6. beauty bt
  7. inner join
  8. boys b
  9. on
  10. bt.boyfriend_id = b.id;

内连接

等值连接

  1. SELECT
  2. last_name,
  3. department_name,
  4. job_title
  5. from
  6. employees e
  7. join
  8. departments d
  9. on
  10. e.department_id = d.department_id
  11. join
  12. jobs j
  13. on
  14. e.job_id = j.job_id
  15. order by
  16. department_name desc;

非等值连接

  1. SELECT
  2. salary,
  3. grade_level
  4. from
  5. employees e
  6. join
  7. job_grades j
  8. on
  9. e.salary between j.lowest_sal and j.highest_sal;

自连接

  1. SELECT
  2. e.last_name yuangong,
  3. m.last_name laoban
  4. from
  5. employees e
  6. join
  7. employees m
  8. on
  9. e.manager_id = m.employee_id
  10. where
  11. e.last_name like('%k%');

外连接:
用于查询一个表中有,另一个表中没有的记录
左外连接

  1. select
  2. bt.name
  3. from
  4. beauty bt
  5. left join
  6. boys b
  7. on
  8. bt.boyfriend_id = b.id
  9. where
  10. b.id is null;

右外连接
全外连接
full join

交叉连接
cross join
证明笛卡尔乘积