题解 | #查找在职员工自入职以来的薪水涨幅情况#
查找在职员工自入职以来的薪水涨幅情况
http://www.nowcoder.com/practice/fc7344ece7294b9e98401826b94c6ea5
将salaries表中的入职工资和离职工资 分别提取出来,连结到employees表的右边,然后做加减
(用了两次salaries表,分别命名为s1、s2)
select e.emp_no, (s2.salary - s1.salary) as growth from employees as e left join salaries as s1 on e.hire_date = s1.from_date left join salaries as s2 on e.emp_no = s2.emp_no where s2.to_date = '9999-01-01' order by growth;
①根据hire_date = from_date 连结入职工资得到的结果:
select * from employees as e left join salaries as s1 on e.hire_date = s1.from_date;
10001|1953-09-02|Georgi|Facello|M|2001-06-22|10001|85097|2001-06-22|2002-06-22
10002|1964-06-02|Bezalel|Simmel|F|1999-08-03|10002|72527|1999-08-03|2000-08-02
10002|1964-06-02|Bezalel|Simmel|F|1999-08-03|10002|72527|1999-08-03|2000-08-02
②继续根据emp_no连结得到的结果
select * from employees as e left join salaries as s1 on e.hire_date = s1.from_date left join salaries as s2 on e.emp_no = s2.emp_no ;10001|1953-09-02|Georgi|Facello|M|2001-06-22|10001|85097|2001-06-22|2002-06-22|10001|85097|2001-06-22|2002-06-22
10001|1953-09-02|Georgi|Facello|M|2001-06-22|10001|85097|2001-06-22|2002-06-22|10001|88958|2002-06-22|9999-01-01
10002|1964-06-02|Bezalel|Simmel|F|1999-08-03|10002|72527|1999-08-03|2000-08-02|10002|72527|1999-08-03|2000-08-02
10002|1964-06-02|Bezalel|Simmel|F|1999-08-03|10002|72527|1999-08-03|2000-08-02|10002|72527|2000-08-02|2001-08-02
④最后用where进行限制,在select中进行加减