Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda