尝试在需要加入5个表的地方编写SQL查询

时间:2017-03-11 00:47:51

标签: sql join

我对SQL很新。我正在编写一个查询脚本,我需要从五个表中加入日期。我没办法测试这个,所以我不确定它是否正确。

SELECT  tblQCC.CSN, 
        tblQCC.question_id, 
        tblQCC.answer, 
        tblEncounters.CSN, 
        tblEncounters.department_id, 
        tblEncounters.prc_id, 
        tblEncounters.patient_id, 
        tblEncounters.account_id, 
        tblEncounters.ser_id, 
        tblEncounters.visit_date, 
        tblPatient.patient_id, 
        tblPatient.patient_name_last, 
        tblPatient.patient_name_first, 
        tblPatient.MRN, 
        tblPatient.DOB, 
        tblAccount.account_id, 
        tblAccount.benefit_plan_name, 
        tblSer.ser_id,  
        tblSer.provider_name
FROM    theQCC
JOIN    tblEncounters 
ON      tblQCC.CSN = tblEncouter.CSN
JOIN    tblPatient 
ON      tblEncounters.patient_id = tblPatient.patient_id
JOIN    tblAccount 
ON      tblEncounters.account_id = tblAccount.account_id
JOIN    tblSer 
ON      tblEncounters.ser_id = tblSer.ser_id
WHERE   tblEncounters.depatement_id = 500
AND     tblQCC.answer = ‘yes’
AND     tblEncounters.visit_date <= 2016-12-10;

1 个答案:

答案 0 :(得分:0)

首先,什么数据库?

指定您的JOIN。他们是INNER,OUTER,LEFT,RIGHT吗?

您是否有可接受空值的表格?

日期部分的引用......等等。