2016-01-22 67 views
1

我正在使用MySQL。以下是我的SQL语句。 timetabledistribution是我的交易表。这工作正常。但是这是返回NULL记录时teacherId = NULLtimetabledistribution表中有记录。 我相信这是因为SQL语句中有一个INNER JOIN teacher_profiles c ON a.teacherId = c.teacherId。 如果teacherId = NULL,那么有没有可能避免或忽略此JOIN?或任何其他更好的办法...INNER JOIN导致NULL RECORD SET

SELECT 
    CONCAT(a.periodWeekDayId, '.', a.periodId) as Id, 
    a.periodId, 
    a.periodWeekDayId, 
    a.subjectId, 
    b.subjectShortName, 
    a.teacherId, 
    CONCAT(c.teacherFirstName , ' ', COALESCE(c.teacherMiddleName, ''), ' ', c.teacherLastName) as teacherName , 
    e.classStd, 
    f.sectionName, 
    g.templateName, 
    h.periodLabel, 
    h.periodStartTime, 
    h.periodEndTime, 
    i.periodWeekDayName, 
    a.timeTableClassSectionId 
FROM timetabledistribution a 
    INNER JOIN subject_master b ON a.subjectId = b.subjectId 
    INNER JOIN teacher_profiles c ON a.teacherId = c.teacherId 
    INNER JOIN TimeTableClassSection d ON a.TimeTableClassSectionId = d.TimeTableClassSectionId 
    INNER JOIN class_master e ON d.classId = e.classId 
    INNER JOIN section_master f ON d.sectionId = f.sectionId 
    INNER JOIN TimeTableTemplate g ON d.timeTableTemplateId = g.timeTableTemplateId 
    INNER JOIN templatePeriodStructure h ON a.periodId = h.periodId 
    INNER JOIN templateWeekDaysStructure i ON a.periodWeekDayId = i.periodWeekDayId 
WHERE A.TimeTableClassSectionId='46' 

我想现场的其余部分应该是除了老师详细的teacherId返回NULL的特定记录。 但是查询忽略了整个记录。

Id periodId periodWeekDayId ….. 
1 12  1 

但得到

Id periodId periodWeekDayId ….. 
NULL NULL  NULL 
+2

我有点困惑.... 你试过使得JOIN教师配置文件的LEFT OUTER 因此改变........ LEFT OUTER JOIN teacher_profiles c ON a.teacherId = c.teacherId – AntDC

+0

有意义..我想我错过了..谢谢你的建议 – fresher

+0

@AntDC把它放到你的答案中。 – Barmar

回答

1

很高兴帮助......

您是否尝试过让JOIN教呃型材左外所以改变........

LEFT OUTER JOIN teacher_profiles c ON a.teacherId = c.teacherId 
0

看到你正在推动这种从timetabledistribution表肯定,你可以简单地追加到WHERE条件.... 所以它变得......

.. 
.. 
WHERE A.TimeTableClassSectionId='46' 
AND A.teacherID IS NOT NULL 
+0

它仍然给我NULL记录SET。我希望剩下的字段应该返回,除了教师详细信息,因为teacherId对于该特定记录是NULL。 – fresher

+0

如果有帮助,请在上面添加更多描述 – fresher