使用MySQL加入多个表

时间:2014-03-04 07:46:04

标签: mysql

我正在尝试加入这些表:

Tables in MySQL Workbench

我正在尝试构建的查询是分配给患者的顾问,要获取顾问名称,需要根据员工ID(STA_id)在员工和顾问身上加入JOIN。然后根据顾问ID(CON_id)加入顾问到患者。

这是我到目前为止所生成的内容:

SELECT CONCAT(Patient.PAT_f_name,' ',Patient.PAT_l_name) AS Patient_Name, 
       CONCAT(Staff.STA_f_name,' ',Staff.STA_s_name) AS Consultant_Name
FROM up637415_hospital.Patient
JOIN up637415_hospital.Consultant ON 
     up637415_hospital.Patient.CON_id=up637415_hospital.Consultant.CON_id
JOIN up637415_hospital.Staff ON 
     up637415_hospital.Consultant.STA_id=up637415_hospital.Consultant.STA_id

查询输出以下结果: Results of Query

重复患者名称。

任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

我设法使用以下查询解决了这个问题:

SELECT CONCAT(Patient.PAT_f_name,' ',Patient.PAT_l_name) AS Patient_Name, 
CONCAT(Staff.STA_f_name,' ',Staff.STA_s_name) AS Consultant_Name
    FROM up637415_hospital.Patient
JOIN up637415_hospital.Consultant ON 
     up637415_hospital.Patient.CON_id=up637415_hospital.Consultant.CON_id
JOIN up637415_hospital.Staff ON 
     up637415_hospital.Consultant.STA_id=up637415_hospital.Consultant.STA_id
WHERE Staff.STA_id=Consultant.STA_id AND Consultant.CON_id=Patient.CON_id

我只需要使用where子句。