49 - 超过经理收入的员工
Last updated
Was this helpful?
Last updated
Was this helpful?
Employee
表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。
+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+
给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。
+----------+ | Employee | +----------+ | Joe | +----------+
通过id找到对应的manager,然后对比他们的salary
作者:LeetCode 链接:
Runtime: 320 ms, faster than 52.18% of MySQL online submissions forEmployees Earning More Than Their Managers.
Memory Usage: N/A
这是用同一个表做了两次select,结果是这个表每个条目的排列组合:
在这个基础上,再用where筛选
最后再让输出的内容加一个别名。
神奇的是,这个能在定义别名之前,先用a。估计解析的时候,是先解析的from,再解析的select。
判断条件多判断一下manageId
是否存在。
Runtime: 299 ms, faster than 83.22% of MySQL online submissions forEmployees Earning More Than Their Managers.
Memory Usage: N/A
Runtime: 336 ms, faster than 43.06% of MySQL online submissions forEmployees Earning More Than Their Managers.
Memory Usage: N/A
好像用on和where效果是一样的
Runtime: 311 ms, faster than 63.59% of MySQL online submissions forEmployees Earning More Than Their Managers.
Memory Usage: N/A
Runtime: 289 ms, faster than 95.16% of MySQL online submissions forEmployees Earning More Than Their Managers.
Memory Usage: N/A
似乎join比where更加高效
作者:dong-fang-xu-ri 链接:
select两张表,相当于