列表

详情


SQL197. 查找当前薪水详情以及部门编号dept_no

描述

有一个全部员工的薪水表salaries简况如下:
emp_no
salary
from_date
to_date
10001
88958 2002-06-22
9999-01-01
10002
72527 2001-08-02
9999-01-01
10003
43311 2001-12-01 9999-01-01

有一个各个部门的领导表dept_manager简况如下:
dept_no
emp_no
to_date
d001 10001 9999-01-01
d002 10003 9999-01-01

请你查找各个部门当前领导的薪水详情以及其对应部门编号dept_no,输出结果以salaries.emp_no升序排序,并且请注意输出结果里面dept_no列是最后一列,以上例子输出如下:
emp_no salary from_date to_date dept_no
10001 88958 2002-06-22 9999-01-01 d001
10003 43311 2001-12-01 9999-01-01
d002

示例1

输入:

drop table if exists  `salaries` ; 
drop table if exists  `dept_manager` ; 
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`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
INSERT INTO dept_manager VALUES('d001',10002,'9999-01-01');
INSERT INTO dept_manager VALUES('d002',10006,'9999-01-01');
INSERT INTO dept_manager VALUES('d003',10005,'9999-01-01');
INSERT INTO dept_manager VALUES('d004',10004,'9999-01-01');
INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01');
INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01');
INSERT INTO salaries VALUES(10003,43311,'2001-12-01','9999-01-01');
INSERT INTO salaries VALUES(10004,74057,'2001-11-27','9999-01-01');
INSERT INTO salaries VALUES(10005,94692,'2001-09-09','9999-01-01');
INSERT INTO salaries VALUES(10006,43311,'2001-08-02','9999-01-01');
INSERT INTO salaries VALUES(10007,88070,'2002-02-07','9999-01-01');

输出:

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Sqlite 解法, 执行用时: 9ms, 内存消耗: 3376KB, 提交时间: 2021-08-08

select s.emp_no,s.salary,s.from_date,s.to_date,dm.dept_no 
from salaries s join dept_manager dm on s.emp_no =dm.emp_no 
order by s.emp_no

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3236KB, 提交时间: 2021-08-29

SELECT s.emp_no, s.salary, s.from_date, s.to_date, d.dept_no
FROM salaries as s
join dept_manager as d on s.emp_no=d.emp_no
where d.to_date in ("9999-01-01","9999-01-01")
group by s.emp_no

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3240KB, 提交时间: 2021-07-29

select s.*,d.dept_no 
from dept_manager d inner join salaries s on  d.emp_no=s.emp_no
order by s.emp_no;

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3240KB, 提交时间: 2021-07-25

 select b.emp_no, b.salary, b.from_date,a.to_date,a.dept_no 
 from dept_manager a left join salaries b
 on a.emp_no=b.emp_no
 order by b.emp_no

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3244KB, 提交时间: 2021-08-03

select  b.*,a.dept_no from dept_manager a inner join salaries b on a.emp_no=b.emp_no order by b.emp_no 

上一题