1

表

嗨,我有以下 3 个表,其中包含示例数据

患者:1,DUMMY PT, 78936, 1987-07-18

Custom_fields : 1 , 1 , '血压' , input , '病历'

患者信息:1、1、80/90 等等等等,1


我想要的是当我非常耐心的页面时,获取 'patient_info' 的所有行,其中 pt_id=patients.patient_id 然后获取 custom_fields.title=cf_id where doctor_id=$id 并显示全部。

从上面给出的数据示例来看,它应该如下所示:

患者: DuMMY 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 中的 $vars),但在“加入患者”附近出现错误

4

3 回答 3

1

你可以使用下面的代码

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';
于 2012-12-11T13:00:07.973 回答
1

我觉得应该是左连接

于 2012-12-11T13:18:54.143 回答
1

尝试这个:

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'
于 2012-12-11T13:10:17.623 回答