如何从一个表中获取另一个表中不存在的行

时间:2017-12-02 23:53:03

标签: java sql jdbc h2

我正在使用下表DDL构建一个Spring启动应用程序

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表中至少有一行存在,那么我需要返回question_id表中所有Questions条目#39; t存在于QuizQuestions表中。

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表中的所有条目。这是amy示例查询

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 个答案:

答案 0 :(得分:3)

类似的东西:

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'
    )