简体   繁体   English

MySQL-如何选择未在另一个表中选择的行

[英]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.

相关问题 如何将数据表中的所选行复制到数据库mysql中的另一个表 - how to copy the selected row in datatable to another table in database mysql mysql-如何从选定的行中选择倾斜行 - mysql - how to select skew row from selected php / pdo / mysql从html表中的select *访问所选行 - php/pdo/mysql accessing selected row from a select * in html table 如何将MySQL数据库表的一行链接到另一个表的另一行 - How to link a row of MySQL database table to another row in another table 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form 如何使用php将来自mysql的选定数据插入mysql中的另一个表? - How to insert selected data from mysql with php to another table in mysql? 如何通过用户名和密钥在MySql表中选择一行并在该行中插入数据 - How to select a row in a MySql table by Username and Key and insert data in that row 如何获取选定的下拉项(ID表行)PHP MYSQL - How to get the selected dropdown item(ID table row) PHP MYSQL 将从MySql填充的下拉列表中选择的整个行值插入另一个MySql表 - Insert entire row values selected from MySql populated drop down list into another MySql Table 如何将一行从一个表移动到mysql中的另一个表? - How to move one row from one table, to another table in mysql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM