难以理解逻辑。收入高于经理的员工。

Trouble understanding logic. Employees who earn more than Manager.

你能帮我理解为什么这个查询对下面的问题不起作用吗: select a.name 作为 'Employee' 从员工 a 加入员工 b 在 a.id=b.managerId 和 a.salary>b.salary

这个解决方案有效,但我不明白为什么我们应该加入 a.managerId=b.Id: select a.name 作为 'Employee' 从员工 a 加入员工 b 在 a.id=b.managerId 和 a.salary>b.salary

你能帮我理解一下吗?

问题来了: Employee table 包含所有员工,包括他们的经理。每个员工都有一个Id,还有一列经理Id。

|编号 |名称 |工资 |经理 ID |

| 1 |乔 | 70000 | 3 |

| 2 |亨利 | 80000 | 4 |

| 3 |山姆 | 60000 |空 |

| 4 |最大 | 90000 |空 |

给定员工 table,编写一个 SQL 查询,找出收入高于其经理的员工。对于以上 table,Joe 是唯一一个收入超过其经理的员工。

|员工 |

|乔 |

如果您使用不同的 table 别名,可能会更容易理解发生了什么。

select emp.name
from employee emp
join employee manager on emp.managerId = manager.id
where emp.salary > manager.salary

加入后,如果您 运行 select * 而不是 select 单个列,您将拥有看起来像这样的记录:

emp.id, emp.name, emp.salary, emp.managerid, manager.id, manager.name, manager.salary, manager.managerid

有了它之后,where 子句就非常简单了。