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
56.获取所有员工的emp_no.md 1.55 KB
Copy Edit Web IDE Raw Blame History
xuthus authored 2019-08-12 23:23 . add:all

获取所有员工的emp_no

题目描述

获取所有员工的emp_no、部门编号dept_no以及对应的bonus类型btype和recevied,没有分配具体的员工不显示

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`));
-- 官网题目缺失表
CREATE TABLE `emp_bonus` (
`emp_no` int not null,
`recevied` datetime not null,
`btype` smallint not null);

答案

select t1.emp_no, dept_no, btype, recevied from (select e.emp_no,dept_no from employees e,dept_emp de where e.emp_no = de.emp_no) t1 left join emp_bonus eb on t1.emp_no = eb.emp_no

题解

题目出的稀巴烂!可以挑出各种错误。

1、获得所有员工对应的emp_no和dept_no

select e.emp_no,dept_no from employees e,dept_manager dm where e.emp_no = dm.emp_no

2、要求btype和recevied字段没有分配具体的员工,这两个字段不显示,明示join操作

Comment ( 0 )

Sign in for post a comment