我有以下数据库:
数据库说明
如果来自未显示的名为patient
的表中的患者包含patient_id和patient_name_en
,则每次访问诊所(桌子访问)时都会出现多种症状,因此他可以看到多位医生/护士每次访问。
每个医生都可以为患者指定多个诊断,从这里您可以看到我们有diagnosis
个表连接到consultation
。
咨询完毕后,医生应说明患者应服用何种药物以及服用多少药物。这是药房的一部分,其中包含3个表格:
medication
:一般药物清单; med_pharmacy
:如果我们有一定数量的特定药物,它将出现在此表中; consultation_med
:它与consultation
表格相关联,此处我们可以指定med_pharmacy_id
以及给予患者多少药片。预期结果
我正在准备一个html页面,使用AJAX-PHP脚本生成报告:
请注意在每次就诊时,患者可以看到多个医生/护士,这就是为什么咨询表每次访问可能包含多行,因此可以开出多种药物。所以在html页面,我们将从红色开始有多行:
我作为查询所做的是以下内容:
select t1.visit_id,
t1.consultation_type,
t3.patient_name,
t3.patient_id,
t3.nationality,
t1.date_of_visit,
t2.complication_name,
t2.consultation_result,
t5.doctor_name,
t6.nurse_name,
t8.med_name,
t4.given_quantity,
t4.medication_collector
FROM visit t1, consultation t2, patient t3
LEFT JOIN consultation_med t4 USING (consultation_id, med_pharmacy_id, clinic_id)
LEFT JOIN doctor_list t5 USING (doctor_list_id, doctor_name, clinic_id)
LEFT JOIN nurse_list t6 USING (nurse_list_id, nurse_name, clinic_id)
LEFT JOIN med_pharmacy t7 USING (med_pharmacy_id, med_id, clinic_ic)
LEFT JOIN medication t8 USING(med_id, med_name, clinic_id)
WHERE t1.visit_id = t2.visit_id
AND t2.consultation_id = t4.consultation_id
AND t1.patient_id = t3.patient_id
AND t1.clinic_id = t3.clinic_id
AND t5.doctor_list_id = t2.doctor_list_id
AND t6.nurse_list_id = t2.nurse_list_id
AND t7.med_id = t8.med_id
AND t1.clinic_id='361' AND t1.patient_id='361-0100' AND t1.visit_id='124'
我一直收到这个错误:
错误代码:1054。'from clause'
中的未知列'consultation_id'
然后我尝试了以下查询,但仍未获得所需的结果:
select t1.visit_id,
t1.consultation_type,
t3.patient_name_en,
t3.patient_id,
t3.nationality,
t1.date_of_visit,
t2.complication_name,
t2.consultation_result,
t5.doctor_name,
t6.nurse_name,
t8.med_name,
t4.given_quantity,
t4.medication_collector
FROM visit t1
LEFT JOIN consultation t2 ON t2.consultation_id = t4.consultation_id, patient t3
LEFT JOIN consultation_med t4
LEFT JOIN doctor_list t5 ON t5.doctor_list_id = t2.doctor_list_id
LEFT JOIN nurse_list t6 ON t6.nurse_list_id = t2.nurse_list_id
LEFT JOIN medication t8 ON t1.clinic_id = t8.clinic_id
LEFT JOIN med_pharmacy t7 ON t7.med_id = t8.med_id
WHERE t1.visit_id = t2.visit_id
AND t1.patient_id = t3.patient_id
AND t1.clinic_id = t3.clinic_id
AND t1.clinic_id='361' AND t1.patient_id='361-0100' AND t1.visit_id='124'
这些带有多个连接的大型查询对我来说是新的。
答案 0 :(得分:1)
试试这个,我已经将一些案例移出了WHERE子句,而是将它们替换为表连接。当你加入尚未声明的表格时,也会重新连接一些连接。
select t1.visit_id,
t1.consultation_type,
t3.patient_name_en,
t3.patient_id,
t3.nationality,
t1.date_of_visit,
t2.complication_name,
t2.consultation_result,
t5.doctor_name,
t6.nurse_name,
t8.med_name,
t4.given_quantity,
t4.medication_collector
FROM visit t1
LEFT JOIN consultation t2 ON t1.visit_id = t2.visit_id
LEFT JOIN consultation t3 ON t1.patient_id = t3.patient_id AND t1.clinic_id = t3.clinic_id
LEFT JOIN consultation_med t4 ON t2.consultation_id = t4.consultation_id
LEFT JOIN doctor_list t5 ON t5.doctor_list_id = t2.doctor_list_id
LEFT JOIN nurse_list t6 ON t6.nurse_list_id = t2.nurse_list_id
LEFT JOIN medication t8 ON t1.clinic_id = t8.clinic_id
LEFT JOIN med_pharmacy t7 ON t7.med_id = t8.med_id
WHERE t1.clinic_id='361'
AND t1.patient_id='361-0100'
AND t1.visit_id='124'