Reputation: 1018
I have two tables
questions table like below
and answers table like below
every question has multiple answers
How can I make query to return the result as below table
Upvotes: 2
Views: 114
Reputation: 7986
You have to order by two columns - q_id
and ans_id
. Since in question_tb
there is no ans_id
field you can put 0
instead.
select t.id, t.q_content from
(
select q_id, q_content, 0 k, q_id id from question_tb
union
select ans_q_id, ans_content, ans_id, ans_id from answer_tb
) t order by t.q_id, t.k
Upvotes: 3
Reputation: 5722
Hmmm.. I think you need to re-think your structure a bit: the last table (or query result) needs a column to designate which is the question, and which are the answers, and also to indicate the correct answer.
Also, I assume that each set of questions (and answers) will be tied to a particular user, so you will need a user_key in the answer table, as well.
SELECT Q.q_content AS question,
ANS.ans_content AS answer,
ANS.is_correct,
ANS.user_id,
FROM Question_TB Q
INNER JOIN tb_answer ANS
ON ANS.ans_q_id = Q.q_id
ORDER BY ANS.user_id, Q.q_id
Upvotes: -1