Mysql 导致空记录集的内部联接
我正在使用MySQL。下面是我的SQL语句Mysql 导致空记录集的内部联接,mysql,Mysql,我正在使用MySQL。下面是我的SQL语句timetabledistribution是我的事务表。这很好用。但当timetabledistribution表中存在teacherId=NULL的记录时,这将返回NULL记录。 我相信这是因为在SQL语句中a.teacherId=c.teacherId上有一个内部连接teacher\u profiles c。 如果此类记录的teacherId=NULL,是否有可能避免或忽略此连接?或者其他更好的方法 SELECT CONCAT(a.peri
timetabledistribution
是我的事务表。这很好用。但当timetabledistribution
表中存在teacherId=NULL
的记录时,这将返回NULL
记录。
我相信这是因为在SQL语句中a.teacherId=c.teacherId上有一个内部连接teacher\u profiles c。
如果此类记录的teacherId=NULL,是否有可能避免或忽略此连接?或者其他更好的方法
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'
我希望该字段的其余部分应该返回,除了teacherdetail,因为对于该特定记录,teacherId为NULL。
但是查询忽略了整个记录
Id periodId periodWeekDayId …..
1 12 1
但是
Id periodId periodWeekDayId …..
NULL NULL NULL
既然您是从timetabledistribution表驱动这个,那么您当然可以简单地附加到where条件。。。。
所以它变成了
..
..
WHERE A.TimeTableClassSectionId='46'
AND A.teacherID IS NOT NULL
很高兴它有帮助
您是否尝试将加入教师配置文件设置为左侧外部,以便更改为
LEFT OUTER JOIN teacher_profiles c ON a.teacherId = c.teacherId
它仍然给我空记录集。我希望该字段的其余部分应该返回,但教师详细信息除外,因为该特定记录的teacherId为空。如果有帮助,请在上面添加更多说明我有点困惑。。。。您是否尝试将加入教师配置文件设置为左侧外部,以便更改为。。。。。。。。左外连接a.teacherId=c.teacherId上的教师\ U配置文件c有意义。。我想我错过了。。谢谢你advise@AntDC把这个写进你的答案里。