sql - 选择既是经理又在经理手下工作的员工
问题描述
我正在尝试构建一个查询来选择所有员工,他们是经理,但也在经理下工作。
例如在下表中
ID | 姓名 | manager_id |
---|---|---|
1 | 更新鲜 | 2 |
2 | 组长 | 3 |
3 | 经理 | 空值 |
我只想获得团队负责人,作为我的选择查询的结果,我尝试使用以下查询,但出现错误。我也不确定这是否是正确的方法。
select e.id, e.name, m.name as mgr_name,
case when EXISTS (select 1 from emps where emps.manager_id = e.id) then 1
else 0 end as isMgr
from emps e
join emps m on e.mgr_id = m.id
where e.manager_id is not null and e.isMgr = 1
错误:
列名“isMgr”无效。
解决方案
我只会使用exists
和过滤:
select e.*
from emps e
where e.manager_id is not null and
exists (select 1
from e e2
where e2.manager_id = e.id
);
推荐阅读
- generics - Rust:泛型必须实现 &xx[...]
- admob - Admob 插页式接收请求但不填充广告
- msbuild - 错误 MSB4018:“CopyRefAssembly”任务意外失败
- blockchain - 是否有可能创建一个循环区块链?
- rust - 使用迭代器接口链接选项和结构字段的问题
- java - 用 JTextField 值画一条线,我做错了什么?
- list - 如何在此函数中将 Traversable 类型解释为 Applicative?
- c++ - 在 C++ 中实现双重调度的问题
- typescript - 如何在打字稿转换器中使用插入的导入
- scala - akka 集群:配置种子节点