我有以下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),问题可能有父问题。
我想摆脱子查询。这样做的正确方法是什么?
答案 0 :(得分: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;