我有一个表格问题,有两种 os 问题,T/F 问题和多项选择问题。是否可以制作一个动态表,其中包含问题的一般列以及 T/F 问题和多项选择题的特定列?
谁能帮助我如何创建模型来解决这个问题?谢谢 。
此解决方案同时处理 T/F 问题和多项选择题:
在 MySQL 中的列上不能有动态类型。据我了解,您正在构建某种可定制的琐事。这是一种方法,可让您对问题有不同类型的答案:
您可以使用类似于以下的查询来检索答案类型:SELECT * FROM Questions LEFT JOIN WHERE
您可以使用类似于以下的查询来检索正确答案:
SELECT
Questions.ID,
IF(AnswersTF.ID IS NOT NULL, AnswersTF.Answer, NULL) AS 'TF',
IF(AnswersChoices.ID IS NOT NULL, AnswersChoices.ID, NULL) AS 'Choice',
IF(AnswersNumber.ID IS NOT NULL, AnswersNumber.Answer,NULL) AS 'Number',
FROM
Questions
LEFT JOIN AnswersTF ON
Questions.ID = AnswersTF.QuestionID
LEFT JOIN AnswersChoices ON
Questions.ID = AnswersChoices.QuestionID
LEFT JOIN AnswersNumber ON
Questions.ID = AnswersNumber.QuestionID
WHERE
Questions.ID = ?
或者
SELECT
Questions.ID,
CASE
WHEN AnswersTF.ID IS NOT NULL THEN AnswersTF.Answer
WHEN AnswersChoices.ID IS NOT NULL THEN AnswersChoices.ID
WHEN AnswersNumber.ID IS NOT NULL THEN AnswersNumber.Answer
ELSE NULL
END AS 'Answer'
FROM
Questions
LEFT JOIN AnswersTF ON
Questions.ID = AnswersTF.QuestionID
LEFT JOIN AnswersChoices ON
Questions.ID = AnswersChoices.QuestionID AND
AnswersChoices.IsAnswer = 1
LEFT JOIN AnswersNumber ON
Questions.ID = AnswersNumber.QuestionID
WHERE
Questions.ID = ?
您可以使用类似于以下的查询来检索多项选择:
SELECT
AnswersChoices.ID,
AnswersChoices.Choice,
AnswersChoices.IsAnswer
FROM
Questions
JOIN AnswersChoices ON
Questions.ID = AnswersChoices.QuestionID
WHERE
Questions.ID = ?
大多数时候,关于数据建模的主题有多个很好的答案,所以如果您认为我的答案不合适,请随时评论以指定您的要求。希望这可以帮助!
将您的问题和他们的选择分成不同的表格。第三个表格定义了问题及其选项之间的映射。使您的表格标准化将有助于避免重复选项组,如 True 和 False。
这是表模式的粗略 UML 草图:
您还需要在表中添加主键/外键约束。为简洁起见,我省略了它们。
CREATE TABLE QUESTIONS
(`Question_ID` int, `Question_Text` varchar(50), `Answer_ID` int);
INSERT INTO QUESTIONS
(`Question_ID`, `Question_Text`, `Answer_ID`)
VALUES
(1, 'True/False question?', 1),
(2, 'Multiple-choice question?', 5);
CREATE TABLE OPTIONS
(`Option_ID` int, `Option_Text` varchar(25));
INSERT INTO OPTIONS
(`Option_ID`, `Option_Text`)
VALUES
(1, 'TRUE'),
(2, 'FALSE'),
(3, 'Option 1'),
(4, 'Option 2'),
(5, 'Option 3'),
(6, 'Option 4');
CREATE TABLE QUESTION_OPTIONS
(`QnA_ID` int, `Question_ID` int, `Option_ID` int);
INSERT INTO QUESTION_OPTIONS
(`QnA_ID`, `Question_ID`, `Option_ID`)
VALUES
(1, 1, 1),
(2, 1, 2),
(3, 2, 3),
(4, 2, 4),
(5, 2, 5),
(6, 2, 6);
现在,只需加入表格即可获取问题的所有相关详细信息。
SELECT Option_Text,
CASE
WHEN q.Answer_ID = o.Option_ID THEN 1
ELSE 0
END Is_Answer
FROM QUESTIONS q, OPTIONS o, QUESTION_OPTIONS qo
WHERE qo.Option_ID = o.Option_ID
AND q.Question_ID = qo.Question_ID
AND q.Question_ID = 1
SELECT Option_Text,
CASE
WHEN q.Answer_ID = o.Option_ID THEN 1
ELSE 0
END Is_Answer
FROM QUESTIONS q, OPTIONS o, QUESTION_OPTIONS qo
WHERE qo.Option_ID = o.Option_ID
AND q.Question_ID = qo.Question_ID
AND q.Question_ID = 2
输出:
+--------------+-----------+
| OPTION_TEXT | IS_ANSWER |
+--------------+-----------+
| TRUE | 1 |
| FALSE | 0 |
+--------------+-----------+
+--------------+-----------+
| OPTION_TEXT | IS_ANSWER |
+--------------+-----------+
| Option 1 | 0 |
| Option 2 | 0 |
| Option 3 | 1 |
| Option 4 | 0 |
+--------------+-----------+
如果您愿意,可以在SQL Fiddle调整表格。