したがって、問題の 2 つのテーブルは次のとおりです。
userinfo: id(PK), users_id(FK to users table), name, surname
doctorpatient: id(PK), doctor_id(FK to users table), patient_id(FK to users table)
アイデアは、各医師が doctorpatient テーブルを介して数人の患者に割り当てられるというものです。私がやりたいのは、配列の配列を返すことです。ここで、内側の配列のそれぞれにこれが含まれています。
users_id(doctor), name(doctor), surname(doctor), users_id(patient), name(patient), surname(patient)
これは、純粋な SQL を使用して行うこともできますか? 私はこれを試しました:
SELECT userinfo.users_id,
userinfo.name,
userinfo.surname,
u2.users_id,
u2.name,
u2.surname
FROM doctorpatient
RIGHT OUTER JOIN userinfo
ON doctorpatient.doctor_id = userinfo.users_id
LEFT OUTER JOIN userinfo AS u2
ON doctorpatient.patient_id = u2.users_id
しかし、結合のどの組み合わせを試しても、うまくいきません。私は3つの別々のクエリでデータを取得しようとしましたが、PHPを使用して必要な結果を取得しようとしましたが、どこにも行きませんでした.
編集:私が欲しいのはこれです:
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...
1 人の医師が複数の患者を担当できる場合。私のクエリは次のようになります。
array(
subarray1(patient_id1,
patient_name1,
patient_surname1,
)
subarray2(patient_id2,
patient_name2,
patient_surname2,
)
etc...
しかし、ほとんどのデータは null です。