Sql 两个相同查询的JOIN和WHERE之间的差异

Sql 两个相同查询的JOIN和WHERE之间的差异,sql,join,where,Sql,Join,Where,我很难理解以下两个问题 select QuestionSetQuestionAnswer.* from QuestionSetQuestionAnswer inner join QuestionAnswer on QuestionAnswer.ID = QuestionSetQuestionAnswer.SelectedAnswerID and QuestionAnswer.IsCorrect = 'true' where QuestionSetQuestionAn

我很难理解以下两个问题

select QuestionSetQuestionAnswer.*
from QuestionSetQuestionAnswer
inner join QuestionAnswer 
    on QuestionAnswer.ID = QuestionSetQuestionAnswer.SelectedAnswerID 
        and QuestionAnswer.IsCorrect = 'true' 
where QuestionSetQuestionAnswer.QuestionID = (
                select QuestionID 
                from QuestionSetQuestion 
                where SetID = '45e20157-0030-c58b-55c4-08d11c725bd7'
                                             )
select QuestionSetQuestionAnswer.* 
from QuestionSetQuestionAnswer
inner join QuestionSetQuestion 
    on QuestionSetQuestion.QuestionID = QuestionSetQuestionAnswer.QuestionID 
        and QuestionSetQuestion.SetID = '45e20157-0030-c58b-55c4-08d11c725bd7'
inner join QuestionAnswer 
    on QuestionAnswer.ID = QuestionSetQuestionAnswer.SelectedAnswerID 
        and QuestionAnswer.IsCorrect = 'true'

这两个查询之间有什么区别?它们相同吗?

第一个查询在QuestionSetQuestion中查找集合ID,并期望找到零个或一个匹配的记录。 -如果它找到一条记录,它将显示所有与找到的问题ID匹配的QuestionSetQuestionAnswer,并乘以正确答案的数量(我猜是零或一)。 -如果未找到任何记录,则不显示任何记录。 -如果发现多条记录,则会发生运行时错误。因此,集合ID对于表应该是唯一的。(这看起来有点可疑。)

第二种方法与之相同,只是集合ID上存在多个匹配项。 -如果它找到一个或多个记录,它会显示所有与找到的问题ID匹配的QuestionSetQuestionAnswer,乘以正确答案的数量(我在上面猜测应该是零或一)乘以找到的QuestionSetQuestion的数量(在第一次查询中假设为零或一)。 -如果未找到任何记录,则不显示任何记录

因此,如果我们总是找到一个或零个具有给定集合ID的记录,那么这两个语句的作用是相同的。在我看来,这两个表都写得不好,因为只显示一个表的记录,所以为什么要连接其他表呢?我想这就是我的意思:

select *
from QuestionSetQuestionAnswer
where QuestionID = -- or IN if multiple matches are possible
(
  select QuestionID 
  from QuestionSetQuestion 
  where SetID = '45e20157-0030-c58b-55c4-08d11c725bd7'
)
and exists 
(
  select *
  from QuestionAnswer 
  where ID = QuestionSetQuestionAnswer.SelectedAnswerID and IsCorrect = 'true' 
);