我有 3 张桌子
questions (q_id, q_text)
answers (a_id, a_text, q_id)
correct_answers (q_id, a_id)
对于每个问题,答案表可以有最少 1 个和最多 4 个值
我需要一个纯 SQL 查询(连接或子查询)来获得下表作为结果
result (q_id, q_text, option1, option2, option3, option4, correct)
option1
, option2
, option3
,option4
都属于答案表并且可以是null
,correct
属于correct_answers
表
问题:
q_id q_text
2 Capital of Pakistan is
3 Karachi is in africa
5 New dehli is _____ of india
答案:
a_id a_text q_id
1 Lahore 2
2 Islamabad 2
3 Karachi 2
4 Quetta 2
5 True 3
6 False 3
7 Capital 5
正确答案:
q_id a_id
2 2
3 6
5 7
查询结果:
q_id q_text option_1 option_2 option_3 option_4 correct
2 Capital of Pakistan is Lahore Islamabad Karachi Quetta Islamabad
3 Karachi is in africa True False null null False
5 New dehli is _____ of india Capital Null Null Null Capital