SQL Left Outer Join就像Inner Join一样

时间:2016-10-18 17:38:29

标签: sql left-join outer-join toad

我正在尝试在两个表上执行左外连接(好吧,内联视图和表)。 我想要发生的是列出所有的毕业生(我知道有3815个DISTINCT毕业生)和他们的任何一个注册(可能有0或n个注册)。我得到的只是一份入学毕业生名单(3649名DISTINCT学生)。我不知道在没有从毕业的所有行中查看错误的地方' (我没有创建查看权限,所以这是我的解决方法)。

这是我的代码:

    SELECT C.*, D.FREEZE_EVENT, D.ACADEMIC_PERIOD, D.CAMPUS, D.COLLEGE, D.COLLEGE_DESC,D.MAJOR, D.MAJOR_DESC , D.STUDENT_RATE
FROM 
(SELECT A.STUDENT_LEVEL_DESC, A.CAMPUS, A.CAMPUS_DESC, A.COLLEGE, A.COLLEGE_DESC, A.MAJOR_DESC, A.MAJOR, A.DEGREE_DESC, A.PERSON_UID, A.ID, A.NAME, 
A.OUTCOME_GRADUATION_DATE, A.STATUS, A.GRAD_YEAR, A.TRAINING_LOCATION, B.CITIZENSHIP_TYPE
FROM ACAD_OUTOCME A, PERSON_DETAIL B
WHERE A.STUDENT_LEVEL IN ('02','03') AND A.GRAD_YEAR = '2015' AND A.FREEZE_EVENT = '10TH_SEP2016' AND B.FREEZE_EVENT = '10TH_SEP2016'
AND A.ID = B.ID) C 
LEFT OUTER JOIN ACAD_STUDY D ON
C.CAMPUS = D.CAMPUS 
AND C.COLLEGE = D.COLLEGE 
AND C.MAJOR = D.MAJOR
AND C.PERSON_UID = D.PERSON_UID
WHERE D.FREEZE_EVENT = '10TH_SEP2016' 
ORDER BY C.NAME

有什么建议吗?我正在使用Toad Data Point。我也是工作中的贷款开发人员,因此我没有任何人可以请求帮助解决这个问题,谷歌也让我失望了。

谢谢!

1 个答案:

答案 0 :(得分:2)

WHERE条件移至ON条件:

Select  C.*
      , D.FREEZE_EVENT
      , D.ACADEMIC_PERIOD
      , D.CAMPUS
      , D.COLLEGE
      , D.COLLEGE_DESC
      , D.MAJOR
      , D.MAJOR_DESC
      , D.STUDENT_RATE
From    (Select A.STUDENT_LEVEL_DESC
              , A.CAMPUS
              , A.CAMPUS_DESC
              , A.COLLEGE
              , A.COLLEGE_DESC
              , A.MAJOR_DESC
              , A.MAJOR
              , A.DEGREE_DESC
              , A.PERSON_UID
              , A.ID
              , A.NAME
              , A.OUTCOME_GRADUATION_DATE
              , A.STATUS
              , A.GRAD_YEAR
              , A.TRAINING_LOCATION
              , B.CITIZENSHIP_TYPE
         From   ACAD_OUTOCME A
         Join   PERSON_DETAIL B  On   A.ID = B.ID
         Where  A.STUDENT_LEVEL In ('02', '03')
                And A.GRAD_YEAR = '2015'
                And A.FREEZE_EVENT = '10TH_SEP2016'
                And B.FREEZE_EVENT = '10TH_SEP2016'
        ) C
Left Outer Join ACAD_STUDY D
        On C.CAMPUS = D.CAMPUS
           And C.COLLEGE = D.COLLEGE
           And C.MAJOR = D.MAJOR
           And C.PERSON_UID = D.PERSON_UID
           And D.FREEZE_EVENT = '10TH_SEP2016'
Order By C.NAME;

在<{em> WHERE之后评估OUTER JOIN,这会导致它过滤掉NULL中的LEFT JOIN记录。因此,在LEFT JOIN子句中使用WHERE的右侧表格可以有效地将OUTER JOIN转换为INNER JOIN