gpt4 book ai didi

mysql - SQL 缺少记录问题

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:45 25 4
gpt4 key购买 nike

大家好,对于这个愚蠢的问题,我深表歉意,但我遇到了一个问题,希望你们能给我指出正确的方向——请耐心等待,我是一个完全的初学者。我设置了三个表,大致如下所示:

table 1 quizquestions
Question ID, etc.
Int

table 2 quizanswers
AnswerID, QuestionID,correct Answer
Int Int Bool

table 3 useranswers
UserAnswerID, AnswerID, QuestionID
Int Int Int

我的问题是当我运行这样的查询时:

SELECT A.AnswerID AS Answer, UA.AnswerID AS UserAnswer 
FROM quizanswers AS A, useranswers UA
WHERE A.CorrectAnswer = TRUE
AND UA.QuestionID
AND A.QuestionID = X

查询将返回如下结果:

Answer UserAnswer 
1 1

如果quizanswers 和useranswers 表中都有记录,查询将成功返回结果。但是,问题是当 useranswers 表中没有记录时,不会返回任何结果。如果上面的代码不够清晰,我深表歉意,在此先感谢您。

最佳答案

我不确定我是否理解问题背后的商业案例,但这会为您提供测验答案的记录,即使用户答案中没有相应的记录也是如此:

    SELECT A.AnswerID AS Answer, UA.AnswerID AS UserAnswer FROM quizanswers AS A LEFT JOIN useranswers UA ON UA.QuestionID = A.QuestionD WHERE A.CorrectAnswer = TRUE AND A.QuestionID = X

关于mysql - SQL 缺少记录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com