简体   繁体   中英

subquery or join?

I am using subquery and join to execute this statement: SHOW ALL THE JOB TITLES THAT EXIST IN FINANCE DEPARTMENT (DO NOT REPEAT ANY JOB TITLES)

my Join works and I get right output, but subquery doesn't and I can't see an error output gives me this :

错误信息

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'

If you run the subquery alone it shold show you more than one row.

As you are doing an equal (job_id = something), that something, as it is a query, is has to deliver a single value as a result which implicitly can't come in more than one row).

Without testing, you probably want:

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM