[LeetCode] Employees Earning More Than Their Managers 员工挣得比经理多
2016-04-05 11:21
405 查看
The
Given the
这道题给我们了一个Employee表,里面有员工的薪水信息和其经理的信息,经理也属于员工,其经理Id为空,让我们找出薪水比其经理高的员工,那么就是一个很简单的比较问题了,我们可以生成两个实例对象进行内交通过ManagerId和Id,然后限制条件是一个Salary大于另一个即可:
解法一:
我们也可以不用Join,直接把条件都写到where里也行:
解法二:
参考资料:
https://leetcode.com/discuss/88189/two-straightforward-way-using-where-and-join
LeetCode All in One 题目讲解汇总(持续更新中...)
Employeetable holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+
Given the
Employeetable, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
+----------+ | Employee | +----------+ | Joe | +----------+
这道题给我们了一个Employee表,里面有员工的薪水信息和其经理的信息,经理也属于员工,其经理Id为空,让我们找出薪水比其经理高的员工,那么就是一个很简单的比较问题了,我们可以生成两个实例对象进行内交通过ManagerId和Id,然后限制条件是一个Salary大于另一个即可:
解法一:
SELECT e1.Name FROM Employee e1 JOIN Employee e2 ON e1.ManagerId = e2.Id WHERE e1.Salary > e2.Salary;
我们也可以不用Join,直接把条件都写到where里也行:
解法二:
SELECT e1.Name FROM Employee e1, Employee e2 WHERE e1.ManagerId = e2.Id AND e1.Salary > e2.Salary;
参考资料:
https://leetcode.com/discuss/88189/two-straightforward-way-using-where-and-join
LeetCode All in One 题目讲解汇总(持续更新中...)
相关文章推荐
- 2015.7.3, 杭州……产品级敏捷案例研究
- 分布式存储技术及应用
- 关于arguments对象以及函数的柯里化;
- 我是一个线程
- [New learn]AutoLayout调查基于code
- 使用的前台开发在线工具
- [OI]省选前模板整理
- 《构建之法》第一至三章
- UVA 10048 A - Wormholes
- 磁盘问题引起的MySQL问题
- java web表单提交ajax参数封装方法
- 制作一个可以滑动操作的 Table View Cell
- protocol buffer安装后使用出错
- TortoiseGit 使用教程
- android基础部分再学习---再谈Service进程服务通信
- assert()
- Explorer.exe 的命令行参数
- mvc 设置默认页技巧
- 《构建之法》第四章读后感
- Android odex文件反编译