内部联接,填充和搜索

时间:2019-03-07 22:46:55

标签: sql-server database join search stored-procedures

我有七个表:

  1. 课程
  2. 课程类别
  3. CourseCourse_Category(这是Course和Course_Category之间的桥接表)
  4. TrainerResourceRequirement
  5. CourseTrainerResourceRequirement(这是Course和TrainerResourceRequirement之间的桥接表)
  6. UnitOfCompetency
  7. CourseUnitOfCompetency(这是Course和UnitOfCompetency之间的桥接表)

我正在尝试建立搜索以按照所选的Course_Category,TrainerResourceRequirement和UnitOfCompetency列出课程。我写了一个存储过程,如下所示:

select CourseName,ClientCourseTitle,CourseDescription,IsTheCourseDisplayedOnWeb, IsTheCourseArchived, minCandidates, maxCandidates,
Course_Category_Name =
STUFF((SELECT ', ' + CC.Course_Category_Name FROM Course_Category as CC
inner join CourseCourse_Category as CCC on CC.Course_CategoryID = CCC.Course_CategoryID
WHERE C.CourseID = CCC.CourseID and CC.Course_CategoryID = @Course_CategoryID
FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, ''),
TrainerResourceName =
STUFF((SELECT ', ' + TRR.TrainerResourceName FROM TrainerResourceRequirement as TRR
inner join CourseTrainerResourceRequirement as CTRR on TRR.TrainerResourceRequirementID=CTRR.TrainerResourceRequirementID
WHERE C.CourseID = CTRR.CourseID and TRR.TrainerResourceRequirementID = @TrainerResourceRequirementID
FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, ''),
UoC =
STUFF((SELECT ', ' +
UoC.UnitOfCompetencyCode + ' - ' + UoC.UnitOfCompetencyTitle as UnitofCompetencyTitle
FROM UnitOfCompetency as UoC
inner join CourseUnitOfCompetency as CUoC on UoC.UnitOfCompetencyID = CUoC.UnitOfCompetencyID
WHERE C.CourseID = CUoC.CourseID and UoC.UnitOfCompetencyID = @UnitOfCompetencyID
FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
FROM Course as C
order by CourseName

但是,当我尝试搜索结果时,结果是这样的:

Search Result

但是结果应仅返回满足所有三个条件的那些行。

我应该如何重组查询?

提前谢谢

0 个答案:

没有答案