mysql 查询并更改所有null_mysql查询——查找所有员工自入职以来的薪资涨幅情况...

370ac0dcd96f166a4f38a623754eda2d.gif

21930d920b4c9bd1da35f07b6f6f79e9.gif

现有两张表,一张员工信息表(employees)和一张工资表(salaries),需要编写一个查询语句,查找出所有员工自入职以来的薪资涨幅情况,输出员工编号emp_id、员工姓名name和对应的薪水涨幅growth。

员工信息表中包含的字段如下:

emp_id int not null

name varchar(20) not null

hire_date date not null

工资表中包含的字段如下:

emp_id int not null,

salary int not null,

from_date date not null,

to_date date not null

示例数据如下:

员工信息表:

2b404c5a2813dd669e8d78a3e4168032.png

工资表:

67188b17e6347dea9cb325dbe139fa4a.png

方法一:使用链接查询

第一步,查找出每个员工入职时的工资,即工资表中的开始计薪日期等于员工信息表中的雇佣日期。

select e.emp_id,e.name,s.salary

from employees e

inner join

salaries s

on e.emp_id=s.emp_id

and e.hire_date=s.from_date;

e2e657bc54b84d0a3c6988f162c59f5d.png

第二步,查找出每个员工当前的工资,假设当前日期为‘2020-04-03’。

select emp_id,salary

from salaries

where to_date='2020-04-03';

69c62f9339cd05f1e6d4aa0b4fc34c28.png

第三步,连接两个临时表,用当前工资减去入职工资得到每个员工的薪水涨幅growth。

select t1.emp_id,t1.name,t2.salary-t1.salary as growth

from(

select e.emp_id,e.name,s.salary

from employees e

inner join

salaries s

on e.emp_id=s.emp_id

and e.hire_date=s.from_date

)t1

inner join

(

select emp_id,salary

from salaries

where to_date='2020-04-03'

)t2

on t1.emp_id=t2.emp_id

order by growth desc;

aa01bdf3b3bd595a3b480b8cb0bdc3f5.png

方法二:使用from并列查询

select e.emp_id,e.name,s2.salary-s1.salary as growth

from employees e,salaries s1,salaries s2

where e.emp_id=s1.emp_id

and s1.emp_id=s2.emp_id

and e.hire_date=s1.from_date

and s2.to_date='2020-04-03'

order by growth desc;

21930d920b4c9bd1da35f07b6f6f79e9.gif

e3816ed49ec851185539502ae52ad196.png

abc382503e62038cd981501a77ea466c.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值