SQL 25) 获取员工其当前的薪水比其manager当前薪水还高的相关信息

获取员工其当前的薪水比其manager当前薪水还高的相关信息

http://www.nowcoder.com/questionTerminal/f858d74a030e48da8e0f69e21be63bef

方法)子查询分别找出员工&经理工资,再连结两表比较工资

SELECT a2.emp_no, a1.emp_no AS manager_no, a2.emp_salary, a1.manager_salary
FROM
( SELECT s1.salary AS manager_salary, dm.emp_no, dm.dept_no  -- 查询当前manager工资
FROM salaries AS s1, dept_manager AS dm
WHERE s1.emp_no = dm.emp_no
AND s1.to_date = '9999-01-01'
AND dm.to_date = '9999-01-01') a1
JOIN 
( SELECT s.salary AS emp_salary, de.emp_no, de.dept_no  -- 查询员工工资
FROM salaries AS s, dept_emp AS de
WHERE s.emp_no = de.emp_no
AND s.to_date = '9999-01-01'
AND de.to_date = '9999-01-01') a2
ON a1.dept_no = a2.dept_no
WHERE a2.emp_salary > a1.manager_salary;

天知道我报了多少遍错...表一多就容易选错列

21/3/25 更新 更简洁的代码 一遍过

方法)多表连接,连结两次salaries表来分别表示经理和员工的工资

select s1.emp_no, s2.emp_no as manager_no, s1.salary , s2.salary
from dept_emp as de join dept_manager as dm on de.dept_no = dm.dept_no
                    join salaries as s1 on s1.emp_no = de.emp_no
                    join salaries as s2 on s2.emp_no = dm.emp_no
where s2.emp_no != s1.emp_no
and s1.salary > s2.salary
and s1.to_date = '9999-01-01'
and s2.to_date = '9999-01-01';
全部评论
where s2.emp_no != s1.emp_no多余判断,工资不等必定不是同一人
1 回复 分享
发布于 2021-09-24 15:49

相关推荐

孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务