获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date=’9999-01-01’,
结果第一列给出员工的emp_no,
第二列给出其manager的manager_no,
第三列给出该员工当前的薪水emp_salary,
第四列给该员工对应的manager当前的薪水manager_salary
CREATE TABLE dept_emp
(emp_no
int(11) NOT NULL,dept_no
char(4) NOT NULL,from_date
date NOT NULL,to_date
date NOT NULL,
PRIMARY KEY (emp_no
,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
));
思路:
#1.先构建两个表:员工工资表和经理工资表
#2.连接2张表,并且同时要求:salaries.emp_no = ‘9999-01-01’ ,员工工资>经理工资**
select emp_salary.emp_no, manager_no, emp_salary, manager_salary
from (
select de.emp_no,de.dept_no,salary as emp_salary
from dept_emp de join salaries s on de.emp_no = s.emp_no
where s.to_date='9999-01-01'
) as emp_salary
join (
select dm.emp_no as manager_no,dm.dept_no, salary as manager_salary
from dept_manager dm join salaries s on dm.emp_no = s.emp_no
where s.to_date='9999-01-01'
) as manager_salary
on emp_salary.dept_no=manager_salary.dept_no #连接条件为同部门
where emp_salary > manager_salary