Mysql 查询 select 不在另一个 table 中的行 ID
Mysql query select row id that is not in the other table
我必须 tables 'config_questions' 和 'user_question'。
'config_questions' 个字段:
- question_id
- question_text
'user_question' 字段
- question_id
- user_id
mysql 结果(question_text 字段)必须是来自 'config_questions'table 的一个问题,其中 question_id 和 user_id不在 'user_question' table.
我从哪里开始?我如何在一个查询中管理它?
不在
中使用
select question_text from config_questions
where question_id not in (select question_id from user_question);
使用
SELECT question_id, question_text
FROM config_questions
WHERE question_id NOT IN (
SELECT question_id
FROM user_question)
开始一个子selection 给你 user_questions 中的所有 question_ids 并且只有 select 那些没有其中之一的问题(不在 returns 仅与以下模式不匹配的结果)。
如果您真的只想要一个结果,您可以使用
SELECT 前 1 question_id, question_text
我假设您正在寻找给定用户的新问题,因此您只希望返回一个问题。
select min( question_text) from config_questions
where question_id not in (select question_id
from user_question
where user_id = 1);
将在 user_id = 1 时完成。
我必须 tables 'config_questions' 和 'user_question'。
'config_questions' 个字段:
- question_id
- question_text
'user_question' 字段
- question_id
- user_id
mysql 结果(question_text 字段)必须是来自 'config_questions'table 的一个问题,其中 question_id 和 user_id不在 'user_question' table.
我从哪里开始?我如何在一个查询中管理它?
不在
中使用 select question_text from config_questions
where question_id not in (select question_id from user_question);
使用
SELECT question_id, question_text
FROM config_questions
WHERE question_id NOT IN (
SELECT question_id
FROM user_question)
开始一个子selection 给你 user_questions 中的所有 question_ids 并且只有 select 那些没有其中之一的问题(不在 returns 仅与以下模式不匹配的结果)。
如果您真的只想要一个结果,您可以使用 SELECT 前 1 question_id, question_text
我假设您正在寻找给定用户的新问题,因此您只希望返回一个问题。
select min( question_text) from config_questions
where question_id not in (select question_id
from user_question
where user_id = 1);
将在 user_id = 1 时完成。