sql 表的连接与查找

       A、left outer join:

  左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。

  SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

  B:right outer join:

  右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。

  C:full outer join:

  全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。

题目描述

查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_no
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

输入描述:

输出描述:

emp_nosalaryfrom_dateto_datedept_no
10002 72527 2001-08-02 9999-01-01 d001
10004 74057 2001-11-27 9999-01-01 d004
10005 94692 2001-09-09 9999-01-01 d003
10006 43311 2001-08-02 9999-01-01 d002
10010 94409 2001-11-23 9999-01-01 d006
select s.*,d.dept_no 
  from salaries s left join dept_manager d 
     on s.emp_no=d.emp_no 
where d.to_date='9999-01-01' and s.to_date='9999-01-01'

select 选择 s.*   s表中的所有列  d.dept_no   d表中的dept_no列

from salaries s     选择从salsries表进行连接取值    并去别名为  s

left join  左连接   on  s,emp_no=d.emp_no   连接的条件

where   匹配条件   

原文地址:https://www.cnblogs.com/52why/p/7634288.html