题目: 获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date=‘9999-01-01’
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 employees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_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
));
select de.dept_no,de.emp_no,s.salary
from dept_emp de
join salaries s on de.emp_no=s.emp_no
where s.to_date="9999-01-01" and de.emp_no not in (
select m.emp_no
from dept_manager m
where m.to_date="9999-01-01"
)
思路:
- 非manager: emp_no不在manager表里
- 在这里可以不用到employee表
- dept_emp表和salaries表结合,查询员工当前工资:
条件为:
1. 当前,s.to_date=‘9999-01-01’
2. 非manager: emp_no不在manager表里 not in
并且当前不为经理:m.to_date=‘9999-01-01’
转载:https://blog.csdn.net/Maggie_HQQ/article/details/104639894