题目描述
查找各个部门当前(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_no | salary | from_date | to_date | dept_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 as s ,dept_manager as d
where
s.emp_no = d.emp_no
and d.to_date = ‘9999-01-01’
and s.to_date = ‘9999-01-01’
或
SELECT s.*,d.dept_no
FROM salaries as s
join
dept_manager as d
on
s.emp_no = d.emp_no
and d.to_date = ‘9999-01-01’
and s.to_date = ‘9999-01-01’
主要考验两个表的逻辑关系,题目要求是薪水情况以及部门编号,再结合输出情况dept_no 被放到了最后一列。
join on :用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。