Sql 用JOIN/APPLY替换子查询
我有以下SQL语句Sql 用JOIN/APPLY替换子查询,sql,sql-server,join,subquery,apply,Sql,Sql Server,Join,Subquery,Apply,我有以下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 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),问题可以有父问题
我想去掉子查询。正确的方法是什么?
JOIN
tableQuestion
请使用不同的别名再试一次:
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;
@Mahmound,原始查询使用子查询来限制答案(t2)中的结果集,而新存储过程则没有。@Icerman它应该以相同的方式工作,并且它将使用
和p.SharedQuestionId=t2.SharedQeustionId
来限制结果集,您可以发布解释此问题的三个表的一些示例数据吗。这将很有帮助。@Mahmound,我使用示例datd进行了验证。谢谢