簡體   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