我有7张那样的表
诊所
clinic_id clinic_name emailId address
诊所和护士桌之间的一对一映射
护士
nurse_id nurse_name nurse_emailid clinic_id
医生
doctor_id doctor_name doctor_email
诊所和医生之间的多对多映射
Doctor_clinic
doctor_clinic_id doctor_id clinic_id
患者
pattient_id patient_name address
patinet和doctor_clinic之间的多对多映射
Doctor_clinic_patient
patient_dotor_id doctor_clinic_id patient_id
doctor_clinic_patient和doctor_patient_appointment之间的一对多映射
Doctor_patient_appointment
appointment_id appointment_time appointment_status patient_doctor_id
appointment_status有三个值 - '待定''已取消'已完成'
所以,Senario从这里开始
从nurse_id(护士表)中选择clinic_id
然后在clin_id的基础上,我想选择doctor_clinic_id(doctor_clinic表)
然后根据doctor_clinic_id,我想选择patient_id
然后在patient_id的基础上,我想从患者表中选择患者详细信息
问题在于,我只想从患者表中选择那些在状态预约表中预约的患者'
我做了以下查询,哪个工作正常,从db获取患者行。但是我想根据预约状态过滤此患者结果
查询
Select * from patient where patient.patient_id in
(select doctor_patient.patient_id from doctor_patient where
doctor_patient.doctor_clinic_id in
(select doctor_clinic.doctor_clinic_id from doctor_clinic where
doctor_clinic.clinic_id in
(select nurse.clinic_id from nurse where nurse_id = 6) ))
答案 0 :(得分:0)
我想多个JOIN可以在这里工作:
SELECT patients.* FROM patients JOIN (
SELECT DISTINCT(p.patient_id) as pat_id FROM clinic c
JOIN doctor_clinic dc ON c.clinic_id = dc.clinic_id
JOIN doctor_clinic_patient dcp ON dcp.doctor_clinic_id = dc.doctor_clinic_id
JOIN patient p ON p.patient_id = dcp.patient_id
JOIN doctor_patient_appointment dpa ON dcp.patient_doctor_id = dpa.patient_doctor_id
WHERE dpa.status = 'pending'
) tmp ON tmp.pat_id = patients.patient_id