我有三张桌子:学生,问题和答案。答案对学生和问题都有FK。 我在学生和问题上使用了交叉连接:
Create PROCEDURE [dbo].[Fetch_AnswersForStudents]
@studentIds [Int_udtt] READONLY
, @questionIds [Int_udtt] READONLY
AS
BEGIN
SELECT
[tQID].[Id]
, ISNULL([tA].[StudentId], 0) AS StudentId
, [tA].[Answer] AS Answer
FROM
@questionIds AS tQID
CROSS JOIN @studentIds AS tSID
LEFT OUTER JOIN Answer AS tA
ON tA.QuestionId = tQID.Id
AND tA.StudentId = tSID.Id
END
此过程的目的是确定给出的学生/问题是否有相应的答案。我的问题是,有没有办法不使用交叉连接来实现相同的目标?此外,最好返回具有答案条目的集合。