当前位置:   article > 正文

MySQL 多表查询

MySQL 多表查询

 employees 表

departments 表

locations 表

job_grades 表 


两表连接,将 employees 表的 department_id 与 departments 表的 department_id 相关联

  1. SELECT employee_id, department_name
  2. FROM employees, departments
  3. WHERE employees.department_id = departments.department_id;
  4. # 或
  5. SELECT employee_id, department_name
  6. FROM employees JOIN departments
  7. on employees.department_id = departments.department_id;

若查询字段在多个表中都出现,则需指明该字段所在的表,如 department_id SELECT 中需写为 employees. department_id  departments.department_id,或者用表的别名代替表名(若给表起了别名,在 SELECTWHERE 中必须使用别名,相当于别名覆盖了原表名) 

  1. SELECT e.employee_id, e.last_name, d.department_name, l.city
  2. FROM employees e, departments d, locations l
  3. WHERE e.department_id = d.department_id
  4. AND d.location_id = l.location_id;
  5. # 或
  6. SELECT e.employee_id, e.last_name, d.department_name, l.city
  7. FROM employees e JOIN departments d
  8. ON e.department_id = d.department_id
  9. JOIN locations l
  10. ON d.location_id = l.location_id;


等值连接和非等值连接

上述为等值连接

非等值连接

  1. SELECT last_name, salary, garde_level
  2. FROM employees e, job_grades j
  3. WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;


自连接和非自连接

上述为非自连接

自连接,表中自我引用,如 employees 表manager_id 相当于对 employee_id 的引用

  1. SELECT e.employee_id, e.last_name, m.employee_id, m.last_name
  2. FROM employees e, employees m
  3. WHERE e.manager_id = m.employee_id;


内连接和外连接

上述均为内连接:合并具有同一列的两个以上的表的行,结果集中不包含一个表与另一个表不匹配的行

外连接:合并具有同一列的两个以上的表的行,结果集中除了包含一个表与另一个表不匹配的行以外,还查询到左表或右表中不匹配的行

外连接的分类:左外连接(还查询左表不匹配的行)、右外连接、满外连接

左外连接

SQL92语法(MySQL不支持)

  1. SELECT e.employee_id, e.last_name, d.department_name
  2. FROM employees e, departments d
  3. WHERE e.department_id = d.department_id(+);

SQL99语法,可以查询到 emplees 表department_id 为NULL的数据,即查询到所有员工的 department_name

  1. SELECT e.employee_id, e.last_name, d.department_name
  2. FROM employees e LEFT OUTER JOIN departments d
  3. ON e.department_id = d.department_id;
右外连接
  1. SELECT e.employee_id, e.last_name, d.department_name
  2. FROM employees e RIGHT OUTER JOIN departments d
  3. ON e.department_id = d.department_id;
满外连接

MySQL不支持 FULL OUTER JOIN 

  1. SELECT e.employee_id, e.last_name, d.department_name
  2. FROM employees e FULL OUTER JOIN departments d
  3. ON e.department_id = d.department_id;

UNION 操作符

UNION 操作符返回两个查询的结果的并集,去除重复记录

UNION ALL 返回两个查询的结果的并集,不去除重复记录

在实际使用中能用 UNION ALL 就不使用 UNION,因此满外连接使用上图①④进行 UNION ALL 

  1. SELECT e.employee_id, e.last_name, d.department_name
  2. FROM employees e LEFT OUTER JOIN departments d
  3. ON e.department_id = d.department_id
  4. UNION ALL
  5. SELECT e.employee_id, e.last_name, d.department_name
  6. FROM employees e RIGHT OUTER JOIN departments d
  7. ON e.department_id = d.department_id
  8. WHERE e.department_id IS NULL;

SQL99新特性

自然连接 NATURAL JOIN

可以理解为SQL92中的等值连接,会自动查询两张连接表中所有相同的字段,然后进行等值连接

  1. SELECT employee_id, last_name, department_name
  2. FROM employees e JOIN departments d
  3. ON e.department_id = d.department_id
  4. AND e.manager_id = d.manager_id;
  5. # 等同于
  6. SELECT employee_id, last_name, department_name
  7. FROM employees e NATURAL JOIN departments d;
 USING

替换等值连接的 =,如上述查询可以写为

  1. SELECT employee_id, last_name, department_name
  2. FROM employees e JOIN departments d
  3. USING (department_id, manager_id);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/414090
推荐阅读
相关标签
  

闽ICP备14008679号