I'm trying to write an sql statement that will return a question and all its corresponding answers. In the example below I've hard coded the subject_id
but it would be set dynamically.
I want to limit the results to only 5 questions but if I just add LIMIT 5
it limits the whole data returned to only 5 which is not what I want.
SELECT q.id,q.question,a.question_id,a.answer, a.correct
FROM questions q
JOIN answers a ON q.id = a.question_id
WHERE q.subject_id = 18
GROUP BY q.id,a.id
I'd also like to achieve something else but I'm not sure whether it would be better to implement it in the business logic (php). I want to return the correct answer for each question (a.correct = 1) plus a random 3 of the incorrect answers (correct = 0).