如何从一个表中获取另一表中不存在的行
Posted
技术标签:
【中文标题】如何从一个表中获取另一表中不存在的行【英文标题】:How do I get rows from one table that do not exist in another table 【发布时间】:2017-12-02 23:53:03 【问题描述】:我正在使用下表 DDL 构建一个 Spring Boot 应用程序
CREATE TABLE IF NOT EXISTS QUESTIONS(
question_id bigint auto_increment,
question_text varchar(256),
answer boolean, primary key(question_id)
);
CREATE INDEX IF NOT EXISTS question_id_index on Questions(question_id);
和
CREATE TABLE IF NOT EXISTS QuizQuestions(
id bigint auto_increment,
quiz_id varchar(10),
question_id bigint,
FOREIGN KEY(quiz_id) REFERENCES Quiz(quiz_id),
FOREIGN KEY(question_id) REFERENCES Questions(question_id)
)
我需要写一个这样的sql查询
1) 如果QuizQuestions
表中至少存在一行quiz_id
,那么我需要返回Questions
表中不存在于QuizQuestions
表中的所有question_id
条目.
2) 如果QuizQuestions
表中没有特定测验ID 的任何行,那么我想返回Questions
表中的所有行。
我的样本数据如下
INSERT INTO Questions(question_id, question_text, answer) VALUES (1, 'Is 2016 a leap year?', TRUE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(2, 'Is Sunday a weekday?', FALSE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(3, 'Is August the 8th month?', TRUE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(4, 'Is July 25 independence day of United States of America?', FALSE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(5, 'Does January have 31 days in a month?', TRUE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(6, 'Does UK have a monarchy?', TRUE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(7, 'Does Lebron play football?', FALSE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(8, 'Is Los Angeles in New York state?', FALSE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(9, 'Is Canada to the north of United States?', TRUE);
INSERT INTO Questions(question_id, question_text, answer) VALUES(10, 'Does November have 30 days in a month?', TRUE);
和
INSERT INTO QuizQuestions(quiz_id, question_id) VALUES('ABCDE12345', 5);
所以如果我查询quiz_id = ABCDE12345
,我想从Questions
返回所有行,除了question_id=5
的条目,因为它已经存在于QuizQuestions
表中。
如果我要查询QuizQuestions
表中不存在的任何其他测验ID,那么我想返回Question
表中的所有条目。这是我的示例查询
SELECT q.question_id, q.question_text, q.answer FROM Questions q
LEFT JOIN QuizQuestions qq ON q.question_id = qq.question_id WHERE
qq.quiz_id = ?
和
SELECT q.question_id, q.question_text, q.answer FROM Questions q
LEFT JOIN QuizQuestions qq ON
q.question_id = qq.question_id WHERE q.question_id NOT IN
(SELECT qq.question_id FROM Questions WHERE qq.quiz_id = ?)
我已经尝试了我能想到的所有可能的查询,但我无法找到输出。
更新
这是我用来查询数据库的代码。集合输出始终为空。
public QuizQuestion fetchUniqueQuizQuestion(String quizId)
StringBuilder sb = new StringBuilder();
sb.append("SELECT q.question_id, q.question_text, q.answer FROM Questions q ");
sb.append("WHERE NOT EXISTS (SELECT 1 FROM QuizQuestions WHERE question_id = q.question_id ");
sb.append(" AND quiz_id = ?) ");
sb.append("OR NOT EXISTS (SELECT 1 FROM QuizQuestions WHERE quiz_id = ?)");
List<QuizQuestion> questions =
this.jdbcTemplate.query(
sb.toString(), new Object[] quizId, quizId, new RowMapper<QuizQuestion>()
@Override
public QuizQuestion mapRow(ResultSet rs, int rowNum) throws SQLException
QuizQuestion qq = new QuizQuestion();
qq.setQuestionId(rs.getInt("question_id"));
qq.setQuizId(quizId);
qq.setAnswer(rs.getBoolean("answer"));
qq.setDescription(rs.getString("question_text"));
return qq;
);
Collections.shuffle(questions);
return questions.isEmpty() ? null : questions.get(0);
【问题讨论】:
【参考方案1】:类似:
SELECT q.question_id, q.question_text, q.answer
FROM Questions q
WHERE NOT EXISTS
(
SELECT 1
FROM QuizQuestions
WHERE question_id = q.question_id
AND
quiz_id = 'the_quiz_id'
)
OR
NOT EXISTS
(
SELECT 1
FROM QuizQuestions
WHERE quiz_id = 'the_quiz_id'
)
【讨论】:
我不明白这个查询。你介意解释一下吗? 如果问题与指定 quiz_id 的 QuizQuestion 不匹配,则第一个 NOT EXISTS 将返回 true。如果指定的 quiz_id 没有 QuizQuestions,则第二个 NOT EXISTS 返回 true 我在测试模式下运行了查询,但它对我不起作用。 @Kartik - “不起作用”不是一个充分的描述。更加详细一些。你有错误吗?如果是这样,它是什么?你得到错误的结果吗?如果是这样,你得到什么与你期望什么?您可能应该edit your question 提供更多信息。 您好,结果集没有返回任何响应。我得到一个空列表。更新了我的帖子。以上是关于如何从一个表中获取另一表中不存在的行的主要内容,如果未能解决你的问题,请参考以下文章