Score
0
Watch 44 Star 256 Fork 69

xuthus / 数据库SQL实战SQLMIT

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
25.获取员工其当前的薪水比其manager当前薪水还高的相关信息.md 1.92 KB
Copy Edit Web IDE Raw Blame History
xuthus authored 2019-08-03 23:19 . add:22-25

获取员工其当前的薪水比其manager当前薪水还高的相关信息

题目描述

获取员工其当前的薪水比其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`));

答案

select t1.emp_no,t2.manager_no,t1.salary emp_salary,t2.manager_salary from (select de.emp_no,salary from dept_emp de,salaries s where de.emp_no not in (select dept_manager.emp_no from dept_manager) and de.emp_no = s.emp_no and s.to_date = '9999-01-01') t1,(select dm.emp_no manager_no,salary manager_salary,dept_no from dept_manager dm,salaries s where dm.emp_no = s.emp_no and s.to_date = '9999-01-01') t2,dept_emp de where t1.emp_no = de.emp_no and de.dept_no = t2.dept_no and t1.salary > manager_salary

题解

1、获取所有manager的工资信息 t2

select dm.emp_no manager_no,salary manager_salary,dept_no from dept_manager dm,salaries s where dm.emp_no = s.emp_no and s.to_date = '9999-01-01'

2、获取所有普通员工的工资信息 t1

select de.emp_no,salary from dept_emp de,salaries s where de.emp_no not in (select dept_manager.emp_no from dept_manager) and de.emp_no = s.emp_no and s.to_date = '9999-01-01'

3、拼接 t1,t2,dept_emp,通过条件过滤

Comment ( 0 )

Sign in for post a comment