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

244
Vistas
SQL cross join with partial table

I am struggling to get the following SQL query to work:

sql_query = f'''
    INSERT INTO table3(animal_id, fruit_id)
    SELECT table1.id, table2.id
    FROM table1
    CROSS JOIN table2
    WHERE table2.id = "{some_value}";
'''

Basically I want to append to table3 the cross join of some columns from table 1 and table 2, but to limit the joined values from table 2 to those rows where table2.id equals some spefic value. Does anyone know what I'm doing wrong here?

EDIT: so say you had the following three tables:

1
ID ANIMAL
  1    Cat
  2    Dog
2
ID  FRUIT
  1  apple
  2 orange
  3 banana
3
ID ANIMAL  FRUIT
  1   Bird mango
  2   Bird grape

How can I make it that I cross join the animal and fruit columns of tables 1 and 2 and append the result to table 3 but only for those rows of table 1 which equal the value specified in the curly brackets, e.g. here 'Dog'

3
ID ANIMAL   FRUIT
  1   Bird  mango
  2   Bird  grape
  3    Dog  apple
  4    Dog orange
  5    Dog banana
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your code should work if you use single instead of double quotes for the parameter that you pass, but you don't need the CROSS join if you are sure that table2 contains that parameter .
You can simplify the code to:

INSERT INTO table3 (ANIMAL, FRUIT) 
SELECT ANIMAL, 'Dog'
FROM table1;

See the demo.

over 4 years ago · Santiago Trujillo 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