leetcode: Employees Earning More Than Their Managers
2015-11-16 14:30
435 查看
原题如下:
The
Given the
本题有两种思路,一种是表的自身连接不使用join关键字,另一种是使用join关键字,第二种效率比较高。
第一种代码如下,
第二种代码如下,
Mysql还有一个关键字left join,表示不管第二个表中有没有数据都把第一个表中的数据打印出来,leetcode的另一个题目考察了一个用法
题目连接:https://leetcode.com/problems/combine-two-tables/
解答:
select FirstName,LastName,City,State
from Person left join Address on Person.PersonId=Address.PersonId因为不存在重复,所以在select可以不用使用表名作为前缀
Employees Earning More Than Their Managers
The
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 | +----------+
本题有两种思路,一种是表的自身连接不使用join关键字,另一种是使用join关键字,第二种效率比较高。
第一种代码如下,
SELECT e1.Name from Employee e1,Employee e2 where e1.ManagerId=e2.Id and e1.Salary>e2.Salary;表名为Employee,e1和e2分别是为Employee起的别名。
第二种代码如下,
SELECT employer.Name FROM Employee employer JOIN Employee manager ON (employer.ManagerId = manager.Id ) WHERE employer.Salary > manager.Salary ;其中employer和manager也是Employee的别名,与JOIN一起使用的ON表示join的字段。
Mysql还有一个关键字left join,表示不管第二个表中有没有数据都把第一个表中的数据打印出来,leetcode的另一个题目考察了一个用法
题目连接:https://leetcode.com/problems/combine-two-tables/
解答:
select FirstName,LastName,City,State
from Person left join Address on Person.PersonId=Address.PersonId因为不存在重复,所以在select可以不用使用表名作为前缀
相关文章推荐
- 常用javascript表单验证汇总
- 不要哭
- shell 脚本学习2
- 获取DATETIEM控件的界面日期
- 使用maven + eclipse 建置spark基礎環境
- 乒乓球拍板和皮子世界排名
- 女士手机一夜跑流量50GB原因是什么
- 【CentOS 7】Linux 分区方案参考
- 数据结构之(2)链表
- android中handler用法总结
- ViewPager+ImageView 图片滑动预览
- 利用动态代理+注解 实现子线程中执行方法
- jquery动画animate
- popup随鼠标移动,OpenLayers.Control.Measure测量距离
- Visual Studio 2008 添加MScomm控件的方法
- C语言实现数组的奇偶排序
- C++编程思想重点笔记(下)
- 使用github托管项目代码
- Winform退出程序的几种方法对比
- 英伟达硬件加速编解码