2015-04-20 232 views
1

下一個sql語句正常工作。它同時顯示patient_id和serv_name,但我嘗試加入這個SQL語句INNER JOIN SQL QUERY

INNER JOIN Patient P ON (C1.patient_id=P.patient_id) 

顯示patient_name而不是patient_id

SELECT C1.patient_id, S.serv_name 
FROM 
    Checkup_Details C 
    INNER JOIN Services S ON (C.serv_id=S.serv_id), 
    Checkup C1 
WHERE 
    C1.today = DATE() AND C1.check_id=C.check_id 
ORDER BY C.check_id 

因此,如何是我該做的,但我不知道如何exactely。

回答

0

假設領域patient_name是Checkup_Details,你必須把

SELECT C1.patient_name ... 

,而不是從

SELECT C1.patient_id ... 
0

只需添加INNER JOIN子句,以獲得表患者和變化C1的列。 patient_id by P.patient_name。

SELECT P.patient_name, S.serv_name 
FROM 
    Checkup_Details C 
    INNER JOIN Services S ON (C.serv_id=S.serv_id), 
    INNER JOIN Patient P ON (C1.patient_id=P.patient_id) 
    Checkup C1 
WHERE 
    C1.today = DATE() AND C1.check_id=C.check_id 
ORDER BY C.check_id 
0

您不應該混合使用隱式和顯式join語法。一個簡單的規則:從不在from子句中使用逗號:

SELECT C1.patient_id, S.serv_name 
FROM Checkup_Details C INNER JOIN 
    Services S 
    ON C.serv_id = S.serv_id INNER JOIN 
    Checkup C1 
    ON C1.check_id = C.check_id INNER JOIN 
    Patient P 
    ON C1.patient_id = P.patient_id 
WHERE C1.today = DATE() 
ORDER BY C.check_id;