我有一个简单的应用程序,向用户提供多项选择题,并允许他们回答这些问题。这是我的表格:
mysql> describe users;
+-------------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+---------------------+------+-----+---------+----------------+
| user_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| user_status_id | bigint(20) unsigned | NO | MUL | NULL | |
| profile_id | bigint(20) unsigned | YES | MUL | NULL | |
+-------------------------------+---------------------+------+-----+---------+----------------+
mysql> describe multiple_choice_questions;
+----------------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------------+---------------------+------+-----+---------+----------------+
| multiple_choice_question_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| multiple_choice_question_text | varchar(500) | NO | | NULL | |
+----------------------------------+---------------------+------+-----+---------+----------------+
mysql> describe multiple_choice_options;
+------------------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------------+---------------------+------+-----+---------+----------------+
| multiple_choice_option_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| multiple_choice_option_name | varchar(250) | NO | UNI | NULL | |
| multiple_choice_option_label | varchar(250) | NO | UNI | NULL | |
| multiple_choice_option_description | varchar(500) | NO | | NULL | |
+------------------------------------+---------------------+------+-----+---------+----------------+
mysql> describe questions_x_mc_options;
+------------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------+---------------------+------+-----+---------+----------------+
| questions_x_mc_option_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| multiple_choice_question_id | bigint(20) unsigned | NO | MUL | NULL | |
| multiple_choice_option_id | bigint(20) unsigned | NO | MUL | NULL | |
+------------------------------+---------------------+------+-----+---------+----------------+
mysql> describe multiple_choice_responses;
+---------------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------+---------------------+------+-----+---------+----------------+
| multiple_choice_response_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| user_id | bigint(20) unsigned | NO | MUL | NULL | |
| multiple_choice_question_id | bigint(20) unsigned | NO | MUL | NULL | |
| multiple_choice_option_id | bigint(20) unsigned | NO | MUL | NULL | |
+---------------------------------+---------------------+------+-----+---------+----------------+
我试图设计一个查询questions
查询特定user_id
尚未的问题。我最好的尝试是:
SELECT *
FROM multiple_choice_responses
WHERE multiple_choice_question_id NOT IN (
SELECT multiple_choice_question_id
FROM multiple_choice_responses
WHERE user_id = 1
);
但是这总是返回一个空集。我只想要一个SELECT
查询,告诉我特定用户尚未回答哪些问题。 有什么想法吗?
答案 0 :(得分:1)
如果您想要一个尚未提问的问题列表,则无法查询multiple_choice_responses
表。该表包含用户与已询问的问题之间的链接。
相反,查询multiple_choice_questions
表,并过滤掉已经提出的任何问题。
SELECT *
FROM multiple_choice_questions
WHERE multiple_choice_question_id NOT IN (
SELECT multiple_choice_question_id
FROM multiple_choice_responses
WHERE user_id = 1
);