Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
From a join table in MySQL, how to find all the foreign_key1 ids which have foreign_key2 assigned to them ONLY from given list?

I have two tables. Table A and Table B. Both are connected with a many-to-many relationship.

Table A:

ID
---
1
2
3

Table B:

ID
---
4
5
6
7

Table AB:

ID | A_ID | B_ID
----------------
8  | 1    | 4
9  | 1    | 5
10 | 1    | 6
11 | 1    | 7
12 | 2    | 5
13 | 2    | 6
14 | 3    | 6

I want to find all Ids from table A, which have assigned Ids from table B from only given B Ids. For example here, we pass Ids in our queries - [5,6] So it should return all A ids which have either all values from array [5,6] assigned or any no. of values but only from array. If other than array id is also assigned then don't include in result. Here result will be 2 and 3. ( 1 is not because it also has 4,7 assigned to it). I am using Sequelize with typescript.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need a sub select to omit unwanted A_ID. Playground: http://sqlfiddle.com/#!9/524468/3

SELECT DISTINCT A_ID
FROM AB
WHERE B_ID IN (5, 6)
  AND A_ID NOT IN (
    SELECT DISTINCT A_ID
    FROM AB
    WHERE B_ID NOT IN (5, 6)
  )
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!