当前位置:网站首页>Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no

Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no

2022-07-05 07:12:00 Just as young

Title Description

Find the current (dept_manager.to_date=‘9999-01-01’) Lead the current (salaries.to_date=‘9999-01-01’) Salary details and their corresponding department numbers dept_no
( notes : Please use salaries Table is used to query the main table , Output the result with salaries.emp_no Ascending sort , And please note in the output dept_no Column is the last column )

CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL, -- ' Employee number ',
`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, -- ' Department number '
`emp_no` int(11) NOT NULL, -- ' Employee number '
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));

Input description :

 nothing 

Output description :

emp_nosalaryfrom_dateto_datedept_no
10002725272001-08-029999-01-01d001
10004740572001-11-279999-01-01d004
10005946922001-09-099999-01-01d003

Their thinking

Due to the requirements of the topic salaries Table is used to query the main table , Output the result with salaries.emp_no Ascending sort , And please note in the output dept_no Column is the last column .
We use left join Left connecting handle salaries Table as the main table , And then use order by Yes salaries.emp_no Sort in ascending order , And dept_no List as select The last column of .

Implementation code

select s.*, d.dept_no
from salaries as s 
left join dept_manager as d
on s.emp_no = d.emp_no
where d.to_date = '9999-01-01' and s.to_date = '9999-01-01'
order by s.emp_no
原网站

版权声明
本文为[Just as young]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140559380325.html