第四章 多表查询

SQL语句的多表查询方式:

例如:按照department_id查询employee(员工表)和department(部门表)的信息。

方法一:(通用型):SELECT …FROM…WHERE

select e.last_name,e.department_id,d.department_name
	from employee e,department d
	where e.department_id=d.department_id

方法二:SELECT …FROM …NATURAL JOIN…

有局限性:会自动连接两个表中相同的列(可能有多个:department_id和manager_id)

select last_name,department_id,department_name
	from employees
	natural join departments;

方法三:SELECT …JOIN…USING…

有局限性:好于方法二,但若多表的连接列列名不同,此法不合适

select last_name,department_id,department_name
	from employees
	join departments
	using (department_id);

方法四:SELECT …FROM …JOIN…ON…

常用方法:较方法一,更易实现外联接(左、右、满)

select last_name,e.department_id,department_name 
from employees e
join departments d
on e.department_id = d.department_id;

–内连接

​ --等值连接

​ --不等值连接

  1. ​ --非自然连接

    ​ --自连接

–外连接

 --左外连接、右外连接、满外连接

习题:

  1. 多表连接查询时,若两个表有相同的列,必须使用表的别名对列名进行引用,否则出错!

  2. 查询出公司员工的last_name,department_name,city

  select last_name,department_name,city
  	from department d,employees e,location l
  	where d.department_id = e.department_id and d.location_id = l.location_id;
  1. 查询出last_name 为“Chen”的manager的信息。(员工的manager_id是某员工的employee_id)

    0)例如:老张的员工号为:“1001”,我的员工号为:“1002”,

    我的manager_id为“1001” —manager是“老张”

    1).通过两条SQL查询:

  select manager_id 
      from employees
      where lower(last_name)='chen';
      --返回结果为108
  select m.* 
  	from employees e, employees m
  	where e.manager_id =108;

2).通过一条SQL查询(自连接):

select m.*
from employees e,employees m
where e.manager_id = m.employee_id and e.last_name='Chen';
  1. .通过一条SQL查询(子查询):
 select * 
 from employees
 where employee_id=(
 				select manager_id 
     				from employees
     				where last_name='Chen'
 				);
  1. 查询每个员工的last_name和GRADE_LEVEL(在JOB_GRADES 表),-----非等值连接
 select last_name,salary ,grade_level,lowest_sal,highest_sal
 	from employees e,job_grades j
 	where e.salary >=j.lowest_sal and e.salary <=j.highest_sal;
  1. 左外连接和右外连接
  select last_name,e.department_id,department_name
  	from employees e,departments d
  	where e.department_id =d.department_id(+);
  	
  select last_name,d.department_id,department_name
  	from employees e,departments d
  	where e.department_id(+)=d.department_id;
  --理解"(+)"的位置:以左外连接为例,因为左表需要返回更多的记录,右表就需要“加上”更多的记录,所以在右表的链接条件上加上“(+)”
  --注意:1).两边都加上"(+)"符号,会发生语法错误!
  	 --2)这种语法是Oracle所独有,不能在其他数据库中使用。
  1. SQL 99 的左外连接,右外连接,满外连接
--1).
select last_name.department_name
	from employee e left outer join departments d
	on e.department_id = d.department_id;
--2).
select last_name,department_name
	from employees e right join departments d
	on e.department_id = d.department_id ;
--3).
select last_name,department_name
from employees e full join departments d 
on e.department_id = d.department_id;
  1. SQL 99连接Employees表和Department表
  --1).
  select *
  	from employees join departments
  	using (department_id);
  	--缺点:要求两个表中必须有一样的列名。
--2).
select * 
	from employees e join departments d
	on e.department_id =d.department_id;
  --3).多表连接
  select e.last_name,d.department_name,l.city
  	from employees e join departments d 
  	on e.department_id = d.department_id
  	join location l
  	on d.location_id = l.location_id;

你可能感兴趣的:(PL\SQL)