gpt4 book ai didi

mysql - 它没有在查询中显示任何结果

转载 作者:行者123 更新时间:2023-11-29 14:06:58 24 4
gpt4 key购买 nike

SELECT DISTINCT q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, r.ReplyType, 
q.QuestionMarks
FROM Answer an
INNER JOIN Question q ON q.QuestionId = an.QuestionId
JOIN Reply r ON q.ReplyId = r.ReplyId
JOIN Option_Table o ON q.OptionId = o.OptionId

我上面的查询显示了一组空结果,尽管它应该显示一些结果。它应该显示每个不同的问题(我的意思是它应该清楚地显示行,以便不存在在所有列中包含完全相同数据的重复行)任何人都可以修复查询,以便它输出它应该输出的结果输出?下表为:

session 表:

SessionId  SessonName
3 ANDES

问题表:

QuestionId  SessionId   QuestionNo  QuestionContent       NoofAnswers   ReplyId     QuestionMarks   OptionId
1 3 1 What is 2+2? 1 1 5 2
2 3 2 What is 2+2? 1 1 5 2
3 3 3 Name these 2 flowers 2 2 5 4

答案表:

AnswerId (Auto)  QuestionId  Answer
1 1 A
2 1 B
3 2 A
4 2 D
5 3 C

回复表:

ReplyId  ReplyType
1 Single
2 Multiple

选项表:

OptionId OptionType
1 A-C
2 A-D
3 A-E
4 A-F

最佳答案

我稍微改变了你的查询,我得到了结果。首先,我更改为使用LEFT JOIN,然后如果附加表中没有答案等,您将收到返回的问题。其次,由于您使用的是聚合函数 GROUP_CONCAT(),因此需要使用 GROUP BY:

SELECT
q.QuestionContent,
o.OptionType,
q.NoofAnswers,
GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer,
r.ReplyType,
q.QuestionMarks
FROM Question q
LEFT JOIN Answer an
ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r
ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o
ON q.OptionId = o.OptionId
group by q.QuestionContent

参见SQL Fiddle with Demo

这将返回结果:

|      QUESTIONCONTENT | OPTIONTYPE | NOOFANSWERS | ANSWER | REPLYTYPE | QUESTIONMARKS |
----------------------------------------------------------------------------------------
| Name these 2 flowers | A-F | 2 | C | Multiple | 5 |
| What is 2+2? | A-D | 1 | ABD | Single | 5 |

关于mysql - 它没有在查询中显示任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14197749/

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