How to join 2 elements of different tables and that match?

0

I have this relationship diagram, I want to show the names of the teachers and the name of the subjects they taught. I have seen that with some query Join can be done but I have tried several and it throws me an error or shows me only the names of the teachers:

 Select NombreProf, NombreAsig
 from profesor
 inner join asignatura on profesor.RutProf
 in imparte = asignatura.idAsignatura in imparte.

How should the sql query be in this case?

    
asked by CHRISTIAN MARCELO HERRERA ROJA 25.10.2018 в 23:37
source

2 answers

1

You still have to make the Join with the pivot table, since you are doing it directly with the subject, and directly, they have no relation.

SELECT
  NombreProf, NombreAsig 
FROM Profesor
INNER JOIN Imparte ON  Imparte.Profesor_RutProf = Profesor.RutProf
INNER JOIN asignatura ON Imparte.Asignatura_idAsignatura = Asignatura.idAsignatura;
    
answered by 26.10.2018 в 00:13
0

The JOIN (join, combine) SQL statement allows combining records from one or more tables in a relational database. With this operation, each record in table A (Imparts) is combined with the corresponding ones in table B (Teacher and Subject) that satisfy the conditions that are specified in the JOIN predicate. The reserved word ON allows the execution of the comparison of the keys of table A and table B.

Considering this then: select Profesor.NombreProf, Asignatura.NombreAsig from Imparte join Profesor on Profesor.RutProf = Imparte.Profesor_RutProf join Asignatura on Asignatura.idAsignatura = Imparte.Asignatura_idAsignatura;

    
answered by 27.10.2018 в 04:12