[英]MySQL - How to select row were not selected in another table
I am creating multiple choice quiz web app with php and mysql; 我正在使用php和mysql创建多项选择测验Web应用程序; 10 questions for each quiz. 每个测验10个问题。 Each time it will select 10 questions from database randomly and those selected qids are inserting into results table. 每次它将从数据库中随机选择10个问题,并将那些选择的qid插入到结果表中。
Currently, I have two tables; 目前,我有两个表; questions
table for questions and options and results
table to store selected questions. 用于问题和选项的questions
表,以及用于存储选定问题的results
表。
My question is how to select questions that are not exists in results table? 我的问题是如何选择结果表中不存在的问题? I have 100 questions in database, what about if user reaches last(100) question. 我的数据库中有100个问题,如果用户遇到last(100)问题该怎么办。
questions
table: questions
表:
+-------+---------------+----------------+
| qids | q_text | q_options |
+-------+---------------+----------------+
| 1 | example1 |opt1, opt2, opt3|
| 2 | example2 |opt1, opt2, opt3|
| 3 | example3 |opt1, opt2, opt3|
+-------+---------------+----------------+
results
table: results
表:
+----+------------------------------+
| id | qids |
+----+------------------------------+
| 1 | 1,2,3,4,5,6,7,8,9,10 |
| 2 |11,12,13,14,15,16,17,18,19,20 |
+----+------------------------------+
The structure of your results table is not good for this purpose. 结果表的结构不适用于此目的。 You might want to use a hasMany relationship between results and questions. 您可能想在结果和问题之间使用hasMany关系。
You need to store each question asked in a separate row. 您需要将每个问题存储在单独的行中。 For this I have demonstrated a third table (result_questions) that relates results to questions with in a hasMany relationship. 为此,我展示了第三个表(result_questions),该表将结果与hasMany关系中的问题相关联。 That is: each result row has many questions. 也就是说:每个结果行都有很多问题。
questions table: 问题表:
qids, q_text, q_options
results table: 结果表:
id, anyothercolumn
result_questions table: result_questions表:
id, resultid, qid
In the result_questions table the id is auto increasing, the resultid is a foreign key from the results table, the qid is a foreign key from the questions table. 在result_questions表中,id是自动增加的,resultid是结果表中的外键,qid是问题表中的外键。
At the end we would end up with a table looking like this: 最后,我们将得到一个如下表:
result_questions: result_questions:
+----+----------+-----+
| id | resultid | qid |
+----+----------+-----+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 5 |
| 6 | 1 | 6 |
| 7 | 1 | 7 |
| 8 | 1 | 8 |
| 9 | 1 | 9 |
| 10 | 1 | 10 |
| 11 | 2 | 11 |
| 12 | 2 | 12 |
| 13 | 2 | 13 |
| 14 | 2 | 14 |
| 15 | 2 | 15 |
| 16 | 2 | 16 |
| 17 | 2 | 17 |
| 18 | 2 | 18 |
| 19 | 2 | 19 |
| 20 | 2 | 20 |
+----+----------+-----+
Note: If questions are selected randomly the order of qids above would be random and not sequential. 注意:如果随机选择问题,则上面的qid的顺序将是随机的,而不是顺序的。
Then you can use the query: 然后,您可以使用查询:
SELECT * FROM questions
WHERE id NOT IN (SELECT qid FROM result_questions)
ORDER BY RAND() LIMIT 10;
That would give you questions that have not been asked. 那会给您未曾问过的问题。 You would want to store them into the result_questions table after creating a new result row, so they don't get asked again. 创建新的结果行后,您希望将它们存储到result_questions表中,因此不会再被询问。
Also, you might want to refactor how your options are being stored. 另外,您可能想重构选项的存储方式。 Either use separate columns for each option if you know the maximum number of options or use a hasMany relationship for questions to options as well 如果您知道最大数量的选项,则对每个选项使用单独的列,或者对选项的问题也使用hasMany关系
I hope you are able to understand these. 希望您能理解这些内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.