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

241
Views
Combinación cruzada de SQL con tabla parcial

Estoy luchando para que funcione la siguiente consulta SQL:

 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}"; '''

Básicamente, quiero agregar a la tabla 3 la combinación cruzada de algunas columnas de la tabla 1 y la tabla 2, pero limitar los valores combinados de la tabla 2 a aquellas filas donde table2.id es igual a algún valor específico. ¿Alguien sabe lo que estoy haciendo mal aquí?

EDITAR: digamos que tenía las siguientes tres tablas:

 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

¿Cómo puedo hacer que cruce las columnas de animales y frutas de las tablas 1 y 2 y agregue el resultado a la tabla 3 pero solo para aquellas filas de la tabla 1 que igualan el valor especificado entre llaves, por ejemplo, aquí 'Perro'

 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 answers
Answer question

0

Su código debería funcionar si usa comillas simples en lugar de dobles para el parámetro que pasa, pero no necesita la combinación CROSS si está seguro de que table2 contiene ese parámetro.
Puede simplificar el código a:

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

Ver la demostración .

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