题解 | #获取员工其当前的薪水比其manager当前薪水还高的相关信息#
获取员工其当前的薪水比其manager当前薪水还高的相关信息
http://www.nowcoder.com/practice/f858d74a030e48da8e0f69e21be63bef
1.一旦别名,就不能使用之前的字段名
2.de.emp_no,dm.emp_no等,只是标注了表格所属,实际显示的字段名还是emp_no。可以参考select m* from milk_tea as m,选出的字段是不带m的
3.子查询表不可以直接select * ,这样a.emp_no就会有重复的emp_no,参考第2点,本解法必须要给子查询表别名,例如(子查询表)a
select emp_no , manager_no , a.salary emp_salary , b.salary manager_salary from (select de.emp_no emp_no, de.dept_no, de.to_date, salary from dept_emp de join salaries sa on de.emp_no=sa.emp_no where de.to_date='9999-01-01' and sa.to_date='9999-01-01')a join (select dm.emp_no manager_no, dm.dept_no, dm.to_date, salary from dept_manager dm join salaries sa on dm.emp_no=sa.emp_no where dm.to_date='9999-01-01' and sa.to_date='9999-01-01')b on a.dept_no=b.dept_no and a.salary>b.salary
4.如果主表不写a.emp_no 会出现不明确ambiguous报错,如果子表有重复的emp_no,(select *),会出现重复duplicate报错
5.这边salary只有一个,所以可以不用标记
select a.emp_no , b.emp_no , a.salary emp_salary , b.salary manager_salary from (select de.emp_no, de.dept_no, de.to_date, salary from dept_emp de join salaries sa on de.emp_no=sa.emp_no where de.to_date='9999-01-01' and sa.to_date='9999-01-01')a join (select dm.emp_no, dm.dept_no, dm.to_date, salary from dept_manager dm join salaries sa on dm.emp_no=sa.emp_no where dm.to_date='9999-01-01' and sa.to_date='9999-01-01')b on a.dept_no=b.dept_no and a.salary>b.salary