好吧,我会说你的桌子应该是这样的:
+------+------------+--------+--------+--------+--------------+
| ID | Question | Ans1 | Ans2 | Ans3 | CorrectAns |
+------+------------+--------+--------+--------+--------------+
| PKey | text | text | text | text | text |
+------+------------+--------+--------+--------+--------------+
| 1 | π = ? | 3.1415 | 2.2465 | 5.6598 | 3.1415 |
+------+------------+--------+--------+--------+--------------+
您的查询将是:
SELECT (CorrectAns = %enteredanswer%) FROM QuizTable WHERE ID=%questionid%
将 json 转换为该表非常容易,反之亦然。
编辑:对于未定义数量的替代方案:
你可以有一张Question
桌子,还有Options
一张像这样的桌子:
Question:
+-------+------------+---------------+
| QID | Question | CorrectAnsID |
+-------+------------+---------------+
| PKey | text | FKey,integer |
+-------+------------+---------------+
| 1 | π = ? | 2 |
+-------+------------+---------------+
Options:
+-------+--------------+--------------+
| OID | OptionText | QuestionID |
+-------+--------------+--------------+
| PKey | text | FKey,Integer |
+-------+--------------+--------------+
| 1 | 3.5600 | 1 |
+-------+--------------+--------------+
| 2 | 3.1415 | 1 |
+-------+--------------+--------------+
| 3 | 3.4567 | 1 |
+-------+--------------+--------------+
| 4 | 3.7894 | 1 |
+-------+--------------+--------------+
| 5 | 3.9874 | 1 |
+-------+--------------+--------------+
这可能比 json 方法工作得更快,但更难实现(连接和编写的东西),所以如果它是一个快速、较小规模的项目,我会使用 json 方法。