Problem with MySQL query of 3 tables

0

I have a problem with the following query

Select nombre, codigo_cita, nombre_sede, fecha_cita, hora_cita, 
       codigo_sedeFK, codigo_sede 
from pacientes as u 
JOIN citas as h 
on h.codigo_docenteFK = u.codigo_docente    //hasta acá todo bien
join sede as I 
ON I.codigo_sede = 1   

As I insert the headquarters join, it is supposed to return only the data where the headquarters is = 1. But what it generates is that it assigns in some way it assigns all appointments that its headquarters is the 1 that equals a Heredia ... Thanks

    
asked by Oscar Alberto Rodriguez 22.11.2018 в 04:27
source

1 answer

0

That's not how the JOIN works, it's more just right there you apply it correctly with the ON , you could define the relationship, something like this

SELECT  u.nombre, 
        u.codigo_cita, 
        u.nombre_sede, 
        u.fecha_cita, 
        u.hora_cita, 
        u.codigo_sedeFK, 
        I.codigo_sede 
FROM pacientes as u 
    JOIN citas as h  ON h.codigo_docenteFK = u.codigo_docente
    JOIN sede as I  ON I.codigo_sede = u.codigo_sedeFK
WHERE I.codigo_sede = 1  

or if it is only filtered by the id of the site you could not define it if you do not list any field of that table

SELECT  u.nombre, 
        u.codigo_cita, 
        u.nombre_sede, 
        u.fecha_cita, 
        u.hora_cita, 
        u.codigo_sedeFK
FROM pacientes as u 
    JOIN citas as h  ON h.codigo_docenteFK = u.codigo_docente
WHERE u.codigo_sedeFK = 1  

You will see that I filter using codigo_sedeFK because you only filter by the id, if it were by some other fields of sede then if you need the relationship

    
answered by 30.11.2018 / 05:44
source