我有以下查询。它工作时间最长,但有时失败,我无法指出原因。我已经缩小了导致它失败的界限,因为它只发生在我一周一次,但是我的同事已多次报道,是否有更好的方式来写它?
我认为使SQL失败的行是LessonSubject.StartDate IS NULL
,但特别是LessonSubject.StartDate<=?
命令(LessonSubject.EndDate>=? OR LessonSubject.EndDate IS NULL)
返回一些数据。在解析我的查询时,我确实删除了lessonSubject.StartDate
,但我想,一旦我添加了这一行,它将具有相同的行为并破坏我的代码。
我要做的是在?
为空或参数lessonsubject.startdate
小于startdate IS NULL
时检索数据。
非常感谢任何帮助。就像我说它大部分时间都在工作,但偶尔会失败,我无法指出原因,但我怀疑它是SELECT PeriodList.pk_PeriodListID, CASE PeriodList.DayShort
WHEN 'M' THEN '" & $mon & "'
WHEN 'Tu' THEN '" & $tues & "'
WHEN 'W' THEN '" & $wed & "'
WHEN 'Th' THEN '" & $thurs & "'
WHEN 'F' THEN '" & $fri & "'
WHEN 'S' THEN '" & $sat & "'
ELSE '" & $sun & "'
END AS mydate, PeriodList.PeriodNumber, PeriodInformation.StartTime, PeriodInformation.EndTime, SubjectList.SubjectCode , RoomList.RoomCode, Staff.StaffID, Lesson.pk_LessonID, SetList.SetNumber, SetList.SetName, SetList.Form
FROM PeriodList
LEFT JOIN PeriodInformation
ON PeriodList.pk_PeriodListID = PeriodInformation.fk_PeriodListID
LEFT JOIN Lesson
ON Lesson.fk_PeriodListID = PeriodInformation.fk_PeriodListID
LEFT JOIN LessonRoom
ON Lesson.pk_LessonID = LessonRoom.fk_LessonID
LEFT JOIN RoomList
ON LessonRoom.fk_RoomID = RoomList.pk_RoomID
LEFT JOIN LessonStaff
ON Lesson.pk_LessonID = LessonStaff.fk_LessonID
LEFT JOIN Staff
ON LessonStaff.fk_StaffID = Staff.pk_StaffID
LEFT JOIN LessonSubject
ON Lesson.fk_LessonSubjectID = LessonSubject.pk_LessonSubjectID
LEFT JOIN SubjectList
ON LessonSubject.fk_SubjectListID = SubjectList.pk_SubjectListID
LEFT JOIN SetList
ON SetList.pk_SetListID = LessonSubject.fk_SetListID
LEFT JOIN SetMember
ON SetMember.fk_SetListID = SetList.pk_SetListID
WHERE PeriodInformation.Rotation=? AND
PeriodList.VersionCode=? AND
(LessonSubject.StartDate<=? OR LessonSubject.StartDate IS NULL) AND
(LessonSubject.EndDate>=? OR LessonSubject.EndDate IS NULL) AND
Setlist.fk_AcademicYearID=?
条件使它失败。
{{1}}
答案 0 :(得分:0)
Outer Join's
的拇指规则(此处为Left Outer Join
)
如果您要过滤(PeriodInformation.Rotation
,Setlist.fk_AcademicYearID
)右表中的任何记录,则应将过滤器移至ON
条件。当您在Where子句中放入正确的表过滤器时,NULL
值(不匹配的记录)将在比较期间被拒绝
FROM periodlist
LEFT JOIN periodinformation
ON periodlist.pk_periodlistid = periodinformation.fk_periodlistid
AND periodinformation.rotation = ?
LEFT JOIN lesson
ON lesson.fk_periodlistid = periodinformation.fk_periodlistid
LEFT JOIN lessonroom
ON lesson.pk_lessonid = lessonroom.fk_lessonid
LEFT JOIN roomlist
ON lessonroom.fk_roomid = roomlist.pk_roomid
LEFT JOIN lessonstaff
ON lesson.pk_lessonid = lessonstaff.fk_lessonid
LEFT JOIN staff
ON lessonstaff.fk_staffid = staff.pk_staffid
LEFT JOIN lessonsubject
ON lesson.fk_lessonsubjectid = lessonsubject.pk_lessonsubjectid
AND lessonsubject.startdate <=?
AND lessonsubject.enddate >=? LEFT
JOIN subjectlist
ON lessonsubject.fk_subjectlistid = subjectlist.pk_subjectlistid
LEFT JOIN setlist
ON setlist.pk_setlistid = lessonsubject.fk_setlistid
AND setlist.fk_academicyearid = ?
LEFT JOIN setmember
ON setmember.fk_setlistid = setlist.pk_setlistid
WHERE periodlist.versioncode = ?