SQL199. 查找所有员工的last_name和first_name以及对应部门编号dept_no
描述
emp_no | birth_date | first_name | last_name | gender | hire_date |
10001 | 1953-09-02 | Georgi | Facello | M | 1986-06-26 |
10002 | 1964-06-02 | Bezalel | Simmel | F | 1985-11-21 |
10003 | 1959-12-03 | Parto | Bamford | M | 1986-08-28 |
10004 | 1954-05-01 | Christian | Koblick | M | 1986-12-01 |
emp_no | dept_no | from_date | to_date |
10001 | d001 | 1986-06-26 | 9999-01-01 |
10002 | d002 | 1989-08-03 | 9999-01-01 |
last_name | first_name | dept_no |
Facello | Georgi | d001 |
Simmel | Bezalel | d002 |
Bamford | Parto | NULL |
Koblick | Chirstian | NULL |
示例1
输入:
drop table if exists `dept_emp` ; drop table if exists `employees` ; 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 `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`)); INSERT INTO dept_emp VALUES(10001,'d001','1986-06-26','9999-01-01'); INSERT INTO dept_emp VALUES(10002,'d002','1996-08-03','9999-01-01'); INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26'); INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21'); INSERT INTO employees VALUES(10003,'1959-12-03','Parto','Bamford','M','1986-08-28'); INSERT INTO employees VALUES(10004,'1954-05-01','Chirstian','Koblick','M','1986-12-01');
输出:
Facello|Georgi|d001 Simmel|Bezalel|d002 Bamford|Parto|None Koblick|Chirstian|None
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3184KB, 提交时间: 2020-07-07
select employees.last_name, employees.first_name, dept_emp.dept_no from employees left join dept_emp on dept_emp.emp_no = employees.emp_no
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3244KB, 提交时间: 2021-07-31
select last_name,first_name,dept_no from employees left join dept_emp on employees.emp_no = dept_emp.emp_no
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3244KB, 提交时间: 2020-11-23
select a.last_name ,a.first_name ,b.dept_no from employees a left outer join dept_emp b on a.emp_no = b.emp_no
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3252KB, 提交时间: 2021-09-01
select e.last_name,e.first_name,d.dept_no from employees e left join dept_emp d on e.emp_no=d.emp_no
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3252KB, 提交时间: 2020-11-23
select e.last_name,first_name,dept_no from employees e left join dept_emp d on e.emp_no=d.emp_no;