sql - Combine multiple inner joined rows into one row with multiple columns -
i have questions table , answers table has 4 8 answers connected 1 question. when want list of questions , answers, use following code:
select q.questionid, q.question, a.answer question q inner join answer on q.questionid=a.questionid;
this gives me 1 row each answer question being repeated on each row. however, want 1 row per question answers in separate columns. if possible, i'd limit 4 answers. if there more 4, rest should ignored. not important.
the 4 answer columns named "correct", "wrong1", "wrong2" , "wrong3". first 1 in table (with lowest answerid) correct one.
thank help!
select q.questionid, q.question, case <some field> when <condition> a.answer end correct, case <some field2> when <condition2> a.answer end wrong1 ... question q inner join answer on q.questionid=a.questionid group q.questionid
you can group question , define conditioned fields "correct", "wrong1", "wrong2" , "wrong3" columns
Comments
Post a Comment