1

我有三个表:学生,问题和答案。答案对学生和问题都有 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

此过程的目的是确定给出的学生/问题是否有相应的答案。我的问题是,有没有办法不使用交叉连接来实现相同的目标?此外,最好返回一个包含答案条目的集合。

4

0 回答 0