SQL205. 获取所有员工当前的manager
描述
emp_no | dept_no | from_date | to_date |
10001 | d001 | 1986-06-26 | 9999-01-01 |
10002 | d001 | 1996-08-03 | 9999-01-01 |
10003 | d002 | 1995-12-03 | 9999-01-01 |
dept_no | emp_no | from_date | to_date |
d001 | 10002 | 1996-08-03 | 9999-01-01 |
d002 | 10003 | 1990-08-05 | 9999-01-01 |
emp_no | manager |
10001 | 10002 |
示例1
输入:
drop table if exists `dept_emp` ; drop table if exists `dept_manager` ; 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`)); INSERT INTO dept_emp VALUES(10001,'d001','1986-06-26','9999-01-01'); INSERT INTO dept_emp VALUES(10002,'d001','1996-08-03','9999-01-01'); INSERT INTO dept_emp VALUES(10003,'d002','1995-12-03','9999-01-01'); INSERT INTO dept_manager VALUES('d001',10002,'1996-08-03','9999-01-01'); INSERT INTO dept_manager VALUES('d002',10003,'1990-08-05','9999-01-01');
输出:
10001|10002
Sqlite 解法, 执行用时: 9ms, 内存消耗: 3300KB, 提交时间: 2020-10-30
select a.emp_no, b.emp_no as manager_no from dept_emp as a join dept_manager as b on a.dept_no = b.dept_no and b.to_date = '9999-01-01' and a.to_date = '9999-01-01' and a.emp_no <> b.emp_no
Sqlite 解法, 执行用时: 9ms, 内存消耗: 3372KB, 提交时间: 2021-08-09
select a.emp_no as emp_no, b.emp_no as manager from dept_emp a left join dept_manager b On a.dept_no = b.dept_no where a.emp_no != b.emp_no
Sqlite 解法, 执行用时: 9ms, 内存消耗: 3376KB, 提交时间: 2021-08-09
select a.emp_no, b.emp_no from dept_emp a left join dept_manager b on a.dept_no = b.dept_no where a.emp_no <> b.emp_no
Sqlite 解法, 执行用时: 9ms, 内存消耗: 3380KB, 提交时间: 2021-08-09
select a.emp_no, b.emp_no manger from dept_emp a join dept_manager b on a.dept_no = b.dept_no and a.emp_no != b.emp_no
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3296KB, 提交时间: 2020-12-29
SELECT e.emp_no, m.emp_no FROM dept_emp e JOIN dept_manager m ON e.dept_no = m.dept_no WHERE e.emp_no <> m.emp_no AND m.to_date = '9999-01-01';