当前位置: 移动技术网 > IT编程>数据库>MSSQL > 数据库SQL实战

数据库SQL实战

2018年12月27日  | 移动技术网IT编程  | 我要评论

4339小游戏,长尾鬣蜥,生活信息

题目描述

获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'
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 `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`));

输入描述:

输出描述:


dept_no emp_no salary
d001 10002 72527
d004 10004 74057
d003 10005 94692
d002 10006 43311
d006 10010 94409

select d.dept_no,d.emp_no,s.salary
from salaries s join dept_manager d
on s.emp_no = d.emp_no
and d.to_date = '9999-01-01'
and s.to_date = '9999-01-01'
1、先用inner join连接两张表,限制条件是两张表的emp_no相同,即d.emp_no = s.emp_no,并且将salaries用别名s代替,dept_manager用别名d代替 2、根据题意,要获取当前manager的当前salary情况,再加上限制条件d.to_date = '9999-01-01' and s.to_date = '9999-01-01'即可(因为同一emp_no在salaries表中对应多条涨薪记录,而当s.to_date = '9999-01-01'时是他当上manager时的记录)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网