我已经有将近一天的时间坚持了这一点。我对数据库不是很了解,因为我是新手。
我有这样的桌子
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'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句