标签:to_date bsp 员工 var and 情况 aries man null
题目描述
获取所有非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`));
示例:
dept_no | emp_no|salary
d001 |10001 | 88958
d004 |10003 | 43311
解答思路:
先查出所有员工的工资情况,再排除掉非manager员工的记录
1.select salary from salaries s left join dept_emp de on s.emp_no=de.emp_no where
s.to_date=‘9999-01-01‘
2.select de.dept_no,de.emp_no from dept_emp de left join dept_manager dr on
de.emp_no=dr.emp_no where de.to_date=‘9999-01-01‘ and dr.emp_no is null
故答案为:
select de.dept_no,de.emp_no,
(select salary from salaries where
emp_no=de.emp_no and to_date=‘9999-01-01‘) as salary
from dept_emp de left join dept_manager dm
on de.emp_no=dm.emp_no where de.to_date=‘9999-01-01‘ and dm.emp_no is null;
标签:to_date bsp 员工 var and 情况 aries man null
原文地址:https://www.cnblogs.com/fruit1024/p/12294098.html