mysql - Select different values from one table based on another table -


so, 2 tables in question:

userinfo: id(pk), users_id(fk users table), name, surname  doctorpatient: id(pk), doctor_id(fk users table), patient_id(fk users table) 

the idea each doctor assigned few patients via doctorpatient table. want return array of arrays, each of inner arrays contains this:

users_id(doctor), name(doctor), surname(doctor), users_id(patient), name(patient), surname(patient) 

can done using purely sql? tried this:

select userinfo.users_id,         userinfo.name,         userinfo.surname,         u2.users_id,         u2.name,         u2.surname    doctorpatient         right outer join userinfo                       on doctorpatient.doctor_id = userinfo.users_id         left outer join userinfo u2                      on doctorpatient.patient_id = u2.users_id  

but no matter combination of joins try, never comes out right. tried getting data in 3 separate queries , somehow result need using php, got that.

edit: want this:

array( subarray1(patient_id1,            patient_name1,            patient_surname1,            doctor_id1,            doctor_name1,            doctor_surname1) subarray2(patient_id2,            patient_name2,            patient_surname2,            doctor_id1,            doctor_name1,            doctor_surname1)  etc... 

where 1 doctor can have multiple patients. query gets me looks this:

array( subarray1(patient_id1,            patient_name1,            patient_surname1,            ) subarray2(patient_id2,            patient_name2,            patient_surname2,            )  etc... 

but of data null.

i think simple join may sufficient. outer joins appear causing null values because tries treat doctors patients.

select u1.users_id doctor_id,         u1.name doctor_name,         u1.surname doctor_surname,         u2.users_id patient_id,         u2.name patient_name,         u2.surname patient_surname  doctorpatient d join userinfo u1 on d.doctor_id = u1.users_id      join userinfo u2 on d.patient_id = u2.users_id 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -