Nema Ga
Nema Ga

Reputation: 2590

SQL select only if all many to many are found

We have 3 tables:

1: jobs with a single column name

2: workers with a single column name

3: jobs_workers lookup table with job_id and worker_id columns.

Now I got an array of job IDs looking like: [1,2,3] how do I select only workers that are doing ALL 3 jobs?

Upvotes: 0

Views: 23

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269603

You could do:

select jw.worker_id
from job_workers jw
where jw.job_id = any @ids
group by jw.worker_id
having count(*) = array_length(@ids);

Upvotes: 1

Related Questions