想象一下,我有两个表,“departments”表和“employee”表。 此员工表有一个“类别”列。
我想查询选择只有特定类型员工的部门。
谢谢。
答案 0 :(得分:0)
您需要在将这两个表链接在一起的任何列上从您的部门和员工表执行连接。在where子句中,您将指定所需的员工类型。
这将为每位员工返回一行,这可能不是您想要的。您可以在departments表中查找的重要列上使用distinct函数来获得最终答案。
答案 1 :(得分:0)
select distinct dept_id
from employee
where category = 'cat1'
and dept_id not in (select distinct dept_id
from employee
where dept_id <> 'cat1');
答案 2 :(得分:0)
SELECT dept_id
FROM departments
WHERE dept_id NOT IN
(SELECT DISTINCT dept_id
FROM employee
WHERE category_id != @specified_category)
此查询假设没有没有员工的部门,因为它也会返回那些空部门。如果这是一个问题,您可以添加:
AND dept_id IN (SELECT distinct dept_id FROM employee)
答案 3 :(得分:0)
Select d.id_department from departments d where not exists
(Select e.id_employee from employees e where e.category!=your_category and e.id_department=d.id_department)
您还需要验证部门是否有员工。