彩虹制造者
发布于

mySQL 中的 'In' 运算符

Employee:
+-----+-------+------------+-----------+
| id  | name  | department | managerId |
+-----+-------+------------+-----------+
| 101 | John  | A          | null      |
| 102 | Dan   | A          | 101       |
| 103 | James | A          | 101       |
| 104 | Amy   | A          | 101       |
| 105 | Anne  | A          | 101       |
| 106 | Ron   | B          | 101       |
+-----+-------+------------+-----------|

从上面的 Employee 表中,我们需要管理至少 5 名员工的经理的姓名。

我想出了这个查询:

  select name from employee where managerid in (
select managerid from employee where managerid is not null group by managerid having count(*) > 4
);

我预计会出现以下输出:

+------+
| name |
+------+
| John |
+------+

但相反,它抛出了:

  
| name  |
| ----- |
| Dan   |
| James |
| Amy   |
| Anne  |
| Ron   |

where 子句将 managerid 返回为 101。但是最终的 select 并没有选择相应的名称。这哪里出了问题?

浏览 (20)
点赞
收藏
1条评论
Klustron小助手
试试将 where 子句更改为 filter by,而不是 :idmanagerid select name from employee where id in ( select managerid from employee where managerid is not null group by managerid having count(*) > 4 );
点赞
评论