1

我有以下 SQL。

SELECT t0.QustionId, t1.QuestionText, t2.AnswerText FROM
ActiveQuestion AS t0
INNER JOIN Question AS t1
    ON t1.QuestionId = ActiveQuestion.QuestionId
LEFT OUTER JOIN Answer AS t2
    ON t2.SectionId = t0.SectionId
    AND t1.ParentId IN 
            (SELECT QuestionId 
                    FROM Question WHERE SharedQuestionId = t2.SharedQeustionId)

问题可以共享(具有相同的 SharedQuestionId),问题可以有父问题。

我想摆脱子查询。正确的方法是什么?

4

1 回答 1

2

JOIN该表Question再次使用不同的别名:

SELECT 
  t0.QustionId, 
  t1.QuestionText, 
  t2.AnswerText,
  p.questionI ParentQuestionId
FROM ActiveQuestion      AS t0
INNER JOIN Question      AS t1  ON t1.QuestionId      = ActiveQuestion.QuestionId
LEFT OUTER JOIN Answer   AS t2  ON t2.SectionId       = t0.SectionId
LEFT OUTER JOIN Question AS  p  ON t1.ParentId        = p.QuestionId 
                               AND p.SharedQuestionId = t2.SharedQeustionId;
于 2013-01-15T06:41:43.953 回答