指定部门?-SQL

指定部门?-SQL,sql,join,Sql,Join,假设我有两个表,departments表和employee表。 此employee表有一个类别列 我想查询选择只有特定类型员工的部门 谢谢。您需要在将这两个表链接在一起的任何列上执行来自部门和员工表的联接。在where子句中,您将指定所需的员工类型 这将为每个员工返回一行,这可能不是您想要的。您可以在departments表中查找的重要列上使用distinct函数来获得最终答案 select distinct dept_id from employee where category = 'ca

假设我有两个表,departments表和employee表。 此employee表有一个类别列

我想查询选择只有特定类型员工的部门


谢谢。

您需要在将这两个表链接在一起的任何列上执行来自部门和员工表的联接。在where子句中,您将指定所需的员工类型

这将为每个员工返回一行,这可能不是您想要的。您可以在departments表中查找的重要列上使用distinct函数来获得最终答案

select distinct dept_id 
from employee
where category = 'cat1'
and dept_id not in (select distinct dept_id
                    from employee 
                    where dept_id <> 'cat1');
此查询假定没有没有没有员工的部门,因为它还将返回这些空部门。如果这是一个问题,您可以添加:

AND dept_id IN (SELECT distinct dept_id FROM employee)
从不存在的部门d中选择d.id\U部门
从员工e中选择e.id\U员工,其中e.category=您的部门类别和e.id\U部门=d.id\U部门您还需要验证该部门是否有员工

请向我们展示您目前拥有的代码什么是DBMS?查询语法和编写查询的首选方式取决于它。
AND dept_id IN (SELECT distinct dept_id FROM employee)