Left Join returning only certain fields

Asked

Viewed 120 times

0

I have the following tables:

tb_agenda

id_agendaid_medicdatehour

tb_patient

id_patientpatient name

tb_medic

id_medicdoctor’s name

tb_scheduling

id_schedulingid_agendaid_patient

I make the following query

select * from tb_agenda as a left join tb_agendamento as b on (a.id_agenda=b.id_agenda)

I would like to return to me the following fields: id_agendamento, hora, nome_medico, nome_ paciente

  • it returns me the name of tb_agenda , but the schedule table only returns me the id I wanted the names . because in tb_scheduling I have key fk id_patient I wanted her to return me with field_patient . when I do left Join

  • I can’t explain well , but I wanted to display the doctor’s schedules in the schedule table and the patient’s schedule in the schedule table it was viewed in the schedule table grid I use this select * from tb_schedule as a left Join tb_schedule as b on (a. id_agenda=b. id_agenda)

  • thank you worked!!!! thank you very much I will put hands to work now :)

1 answer

0

It would be something like this:

SELECT id_agendamento, hora, nome_medico, nome_paciente FROM tb_agenda AS agenda 
INNER JOIN tb_agendamento AS agendamento ON agenda.id_agenda = agendamento.id_agenda 
INNER JOIN tb_paciente AS paciente ON paciente.id_paciente = agendamento.id_paciente 
INNER JOIN tb_medico AS medico ON medico.id_medico = agenda.id_medico 

Learn more about how to make a relationship here

There are still some addendums on the structure of their tables. It depends on which business rule you intend to implement, but this way in most cases would be better:

tb_agenda

id_agenda:int

date:timestamp

hour:team

tb_patient

id_patient:int

patient name:string

tb_medic

id_medic:int

doctor’s name:string

tb_scheduling

id_scheduling:int

fk_agenda:int

fk_medic:int

fk_patient:int

Browser other questions tagged

You are not signed in. Login or sign up in order to post.