INNER JOIN SQL QUERY

时间:2015-04-20 20:12:49

标签: sql

下一个sql语句工作正常。它显示了patient_id和serv_name,但我尝试显示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

所以我想通过添加这个sql语句来做到这一点

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

但我不知道怎么说。

3 个答案:

答案 0 :(得分:0)

假设字段patient_name在Checkup_Details中,则必须输入

SELECT C1.patient_name ...

而不是

SELECT C1.patient_id ...

答案 1 :(得分:0)

只需添加INNER JOIN子句即可获取表Patient的列,并通过P.patient_name更改C1.patient_id。

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

答案 2 :(得分: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;