如何使所有行匹配+不匹配和不匹配的行列将为空

时间:2014-08-09 21:27:06

标签: sql sql-server

我正在建立一个大学管理系统。这是我的数据库图表

Database Diagram

我正在使用此查询创建搜索表单:

SELECT Distinct    
   TblStudentBioData.RegNo, 
   TblStudentBioData.First_NameUr + SPACE(1)+ TblStudentBioData.Middle_NameUr + SPACE(1) + TblStudentBioData.Last_NameUr AS Name,                                   
   TblStudentBioData.Father_NameUr, 
   Ay.AcademicYearName, 
   Smst.SemName, 
   TBLCOLLEGE.CollegeName,  
   CID.ClassName, 
   TblImages.Images,
   TblStudentBioData.Student_ID,
   TblImages.ImageId,
   Ay.AcademicYearId, 
   Smst.SemesterId,
   TblClassSchedule.ClassSchId        
FROM  
   TblStudentBioData 
LEFT JOIN
   TblStudentDetail ON (TblStudentBioData.Student_ID = TblStudentDetail.Student_ID) 
                      OR (TblStudentBioData.Student_ID != TblStudentDetail.Student_ID)
INNER JOIN
   TBLCFGSEX AS sex ON TblStudentBioData.CfgSexId = sex.CfgSexId 
INNER JOIN
   TBLMARITALSTATUS ON TblStudentBioData.MaritalStatusId = TBLMARITALSTATUS.MaritalStatusId 
INNER JOIN  
   TblStudentSubAss ON TblStudentDetail.StudentDetailID = TblStudentSubAss.StudentDetailID 
INNER JOIN
   TblSubAss ON TblSubAss.SubAssId = TblStudentSubAss.SubAssId 
INNER JOIN 
   TblClassSchedule ON TblStudentDetail.ClassSchId = TblClassSchedule.ClassSchID 
INNER JOIN
   TableClass AS CID ON  TblClassSchedule.ClassID = CID.ClassID 
INNER JOIN
   TblImages ON TblStudentBioData.ImageId = TblImages.ImageId 
LEFT JOIN
   TBLCOLLEGE ON CID.CollegeId = TBLCOLLEGE.CollegeID 
INNER JOIN
   TBLBLOODGROUP BG On TblStudentBioData.BloodID = BG.BloodId 
INNER JOIN
   tableSemAssigning SA On TblClassSchedule.SemAssId = Sa.SemAssId 
INNER JOIN
   TblAcademicYear AY On SA.AcademicYearId = AY.AcademicYearId   
INNER JOIN
   TableSemester Smst On Smst.SemesterId = Sa.SemesterId 

并且此查询为我提供了这样的输出

enter image description here

在结果中第5行不匹配行。

我的问题:如何在图像中提到的非匹配行的列中显示空值?

1 个答案:

答案 0 :(得分:0)

据我所知,就像这样。任何与TblStudentBioData直接相关的东西都是一个内部联接(我无法分辨)但是与TblStudentDetail相关的所有内容都应该是左连接。

另外要注意不要覆盖这些左连接的任何where子句。

SELECT DISTINCT /* I hate distinct, there is probably a better way */

      TblStudentBioData.RegNo
    , TblStudentBioData.First_NameUr + SPACE(1) + TblStudentBioData.Middle_NameUr + SPACE(1) + TblStudentBioData.Last_NameUr AS Name
    , TblStudentBioData.Father_NameUr
    , Ay.AcademicYearName
    , Smst.SemName
    , TBLCOLLEGE.CollegeName
    , CID.ClassName
    , TblImages.Images
    , TblStudentBioData.Student_ID
    , TblImages.ImageId
    , Ay.AcademicYearId
    , Smst.SemesterId
    , TblClassSchedule.ClassSchId
FROM TblStudentBioData
      INNER JOIN TBLCFGSEX AS sex
                  ON TblStudentBioData.CfgSexId = sex.CfgSexId
      INNER JOIN TBLMARITALSTATUS
                  ON TblStudentBioData.MaritalStatusId = TBLMARITALSTATUS.MaritalStatusId
      INNER JOIN TblImages
                  ON TblStudentBioData.ImageId = TblImages.ImageId
      INNER JOIN TBLBLOODGROUP BG
                  ON TblStudentBioData.BloodID = BG.BloodId

      LEFT JOIN TblStudentDetail
                  ON (TblStudentBioData.Student_ID = TblStudentDetail.Student_ID)
      LEFT JOIN TblStudentSubAss
                  ON TblStudentDetail.StudentDetailID = TblStudentSubAss.StudentDetailID
      LEFT JOIN TblClassSchedule
                  ON TblStudentDetail.ClassSchId = TblClassSchedule.ClassSchID

      LEFT JOIN TblSubAss
                  ON TblSubAss.SubAssId = TblStudentSubAss.SubAssId
      LEFT JOIN TableClass AS CID
                  ON TblClassSchedule.ClassID = CID.ClassID
      LEFT JOIN TBLCOLLEGE
                  ON CID.CollegeId = TBLCOLLEGE.CollegeID
      LEFT JOIN tableSemAssigning SA
                  ON TblClassSchedule.SemAssId = SA.SemAssId
      LEFT JOIN TblAcademicYear AY
                  ON SA.AcademicYearId = AY.AcademicYearId
      LEFT JOIN TableSemester Smst
                  ON Smst.SemesterId = SA.SemesterId