子查询还是加入?

subquery or join?

我正在使用子查询和连接来执行这条语句: 显示财务部门中存在的所有职位名称(不要重复任何职位名称)

我的连接有效,我得到了正确的输出,但子查询没有,我看不到错误 输出给我这个:

select DISTINCT(job_title) 
from jobs 
where job_id = (select job_id from employees 
                where department_id = 
                (select department_id from departments
                where department_name like 'finance'))



select DISTINCT(job_title) from jobs j
inner join employees e 
on j.job_id = e.job_id
inner join departments d
on d.department_id = e.department_id
where department_name like 'finance'

如果您 运行 单独使用子查询,它会显示不止一行。

当你做一个相等的(job_id = something)时,那个东西,因为它是一个查询,必须提供一个单一的值作为结果,隐含地不能超过一个行)。

未经测试,您可能需要:

select DISTINCT(job_title) 
from jobs 
where job_id IN (select job_id from employees 
                where department_id IN 
                (select department_id from departments
                where department_name like 'finance'))