数据库进行复杂的3路连接查询

问题描述:

Tables数据库进行复杂的3路连接查询

嗨,我有以下3个表,例如在其内部数据

患者:1,DUMMY PT,78936,1987年7月18日

Custom_fields: ,1, '血压',输入, '病历'

patient_info:1,,九十零分之八十零等等等等,1


我想要的是,当我很有耐心页,是得到所有行“patient_info”,其中pt_id = patients.patient_id然后拿到custom_fields.title = CF_ID其中doctor_id = $ ID 并显示所有。

从它上面的数据应该给出例子是这样的:

病人:虚拟PT的个人资料页: -

血压:80/90等等等等

任何暗示我应该如何加入这张桌子在一起?

注:我想:

SELECT patient_info.info,custom_fields.title FROM patient_info where patient_info.pt_id='8' and custom_fields.id=patient_info.cf_id and custom_fields.doctor_id = '10' 
join patients on patient_info.pt_id=patients.patients_id 
join custom_fields on patient_info.cf_id=custom_fields.id 

,其中10和8已经吉文斯($瓦尔在PHP),但即时得到错误附近 '加入病人'

+0

@ saharsh .->在编辑上加上 – Zalaboza

+0

为什么你要在其他条件之后加入其他表。语法是选择然后从然后加入然后 –

试试这个:

SELECT p.info,cf.title 
FROM patient_info p 
INNER JOIN patients ps ON p.pt_id=ps.patients_id 
INNER JOIN custom_fields cf ON p.cf_id=cf.id 
WHERE p.pt_id='8' AND cf.doctor_id = '10' 
+0

工作:d ..谢谢没有注意到之前加入:/ tyty – Zalaboza

您可以使用下面的代码

select * from patients P,Custom_fields CF,patient_info PI where PI.pt_id=p.patient_id 
and PI.cf_id=CF.id and CF.doctor_id='$id'; 

我觉得应该是Left Join