我已经坚持了差不多一天了。我对数据库不太了解,因为我是新手。
我有一张这样的 table
问题表
question_strand | question_number | question_difficulty
part 1 | 1 | easy
.. | ... | ...
part 1 | 20 | medium
.. | ... | ...
part 10 | 20 | difficult
复合键:question_strand 和 question_number
问题表由 10 个部分组成,每个部分有 20 个项目,每个数字都有难度。此外,该表还包含 Question_items 和其他列,我没有在示例中包含这些列,因为它与问题无关
问题答案表
question_strand | question_number | question_answer | student_name
part 1 | 1 | true | gerald
... | ... | ... | ...
part 1 | 20 | wrong | gerald
复合键question_strand、student_name和question_number
问题答案表也由question_strand和question_number组成,但有question_answer和student_name
我只想选择特定学生的答案。
这就是我想要得到的结果
question_strand | question_number | question_answer | student_name
part 1 | 1 | true | gerald
... | ... | ... | ...
part 1 | 20 | wrong | gerald
part 2 | 1 | null | null
... | ... | ... | ...
part 10 | 20 | null | null
我想获取所有问题,但如果没有与第二个表相对应的任何值,也包括空值
我正在使用 mySQL。提前致谢
最佳答案
您可以简单地使用左连接
SELECT
q.question_strand,
q.question_number,
a.question_answer,
q.student_name
FROM
questions_table q LEFT JOIN answer_table a
ON q.question_strand = a.question_strand AND
q.question_number = a.question_number
如果您想要特定学生的记录
SELECT
q.question_strand,
q.question_number,
a.question_answer,
q.student_name
FROM
questions_table q LEFT JOIN answer_table a
ON q.question_strand = a.question_strand AND
q.question_number = a.question_number AND
q.student_name = 'gerald'
关于mysql - 带有空值的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20449747/