繁体   English   中英

确定MySQL多对多关系中的记录缺失

[英]Determining record absence in MySQL many-to-many relationship

我有一个简单的应用程序,向用户显示多项选择题,并允许他们回答这些问题。 这是我的桌子:

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查询,它告诉我特定用户尚未回答哪些问题。 有任何想法吗?

如果您想要未列出的问题列表,则无法查询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
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM