具有多个条件的多个SQL JOIN

时间:2014-11-14 16:49:11

标签: sql select join

SELECT A.SspStudent, 
       B.StuLastName, 
       B.StuFirstName,
       C.TerDescription, 
       A.SspCourseSubject, 
       A.SspCourseID,
       A.SspCreditHours, 
       A.SspStatus,
       B.StuEmailUNCW, 
       B.StuAdvisorEmail,
       B.StuApplicationStatus,
       D.UseName 
FROM ((tblStudentSchedulePerm A
      INNER JOIN tblStudent B 
           ON (A.SspStudent = B.StuID AND
               B.StuAdmitTerm <> '201520A' AND
               A.SspStatus = 'Schedul' AND 
              (A.SspTerm = '201520A' OR A.SspTerm = '201520B') AND
               A.SspCreditHours <> 0))
     LEFT OUTER JOIN tblTerms C 
           ON A.SspTerm = C.TerTermPart)
     LEFT OUTER JOIN D tblUser
           ON B.StuAdvisorEmail = D.UseEmail;

我试图让这个查询工作,但每次我尝试调整括号时都会出错。 我能得到一些帮助吗?

3 个答案:

答案 0 :(得分:1)

你最好,除了那些括号,你还需要在连接中的子查询

保留括号,除了在OR条件中,试试这个。

SELECT A.SspStudent, 
       B.StuLastName, 
       B.StuFirstName, 
       C.TerDescription, 
       A.SspCourseSubject, 
       A.SspCourseID, 
       A.SspCreditHours, 
       A.SspStatus, 
       B.StuEmailUNCW, 
       B.StuAdvisorEmail, 
       B.StuApplicationStatus, 
       D.UseName 
FROM   tblStudentSchedulePerm A 
       INNER JOIN tblStudent B 
       ON A.SspStudent = B.StuID 
          AND B.StuAdmitTerm <> '201520A' 
          AND A.SspStatus = 'Schedul' 
          AND ( A.SspTerm = '201520A' 
                OR A.SspTerm = '201520B' ) 
          AND A.SspCreditHours <> 0 
       LEFT OUTER JOIN tblTerms C 
                     ON A.SspTerm = C.TerTermPart 
       LEFT OUTER JOIN  tblUser  D
                    ON B.StuAdvisorEmail = D.UseEmail; 

答案 1 :(得分:1)

D tblUser 

应该是:

tblUser D

除此之外,删除所有无用的括号:

SELECT A.SspStudent, 
       B.StuLastName, 
       B.StuFirstName, 
       C.TerDescription, 
       A.SspCourseSubject, 
       A.SspCourseID, 
       A.SspCreditHours, 
       A.SspStatus, 
       B.StuEmailUNCW, 
       B.StuAdvisorEmail, 
       B.StuApplicationStatus, 
       D.UseName 
FROM   tblStudentSchedulePerm A 
         INNER JOIN tblStudent B 
                 ON  A.SspStudent = B.StuID 
                      AND B.StuAdmitTerm <> '201520A' 
                      AND A.SspStatus = 'Schedul' 
                      AND ( A.SspTerm = '201520A' 
                             OR A.SspTerm = '201520B' ) 
                      AND A.SspCreditHours <> 0 
         LEFT OUTER JOIN tblTerms C 
                      ON A.SspTerm = C.TerTermPart
         LEFT OUTER JOIN tblUser D
                      ON B.StuAdvisorEmail = D.UseEmail; 

答案 2 :(得分:1)

我缩进你的查询。这就是我通常开始挑选一些我不熟悉的东西。

SELECT
    A.SspStudent
    , B.StuLastName
    , B.StuFirstName
    , C.TerDescription
    , A.SspCourseSubject
    , A.SspCourseID
    , A.SspCreditHours
    , A.SspStatus
    , B.StuEmailUNCW
    , B.StuAdvisorEmail
    , B.StuApplicationStatus
    , D.UseName
FROM
    (
        (
        tblStudentSchedulePerm A
            INNER JOIN tblStudent B ON
                (
                A.SspStudent = B.StuID
                AND B.StuAdmitTerm <> '201520A'
                AND A.SspStatus = 'Schedul'
                AND (A.SspTerm = '201520A' OR A.SspTerm = '201520B')
                AND A.SspCreditHours <> 0
                )
        )
        LEFT OUTER JOIN tblTerms C ON A.SspTerm = C.TerTermPart
    )
    LEFT OUTER JOIN D tblUser ON B.StuAdvisorEmail = D.UseEmail
;