【发布时间】:2016-04-10 21:37:25
【问题描述】:
好的,所以我一直在尝试从主记录中检索一组子记录。我正在制作一个简单的考试应用程序,并想从问题中检索答案。
我有以下表格:
Questions:
--------------------------------------------------
| question_id | exam_id | question |
--------------------------------------------------
| 1 | 1 | What's 2+2? |
--------------------------------------------------
| 2 | 1 | What's 6+5? |
--------------------------------------------------
Answers:
--------------------------------------------------
| answer_id | question_id | answer |
--------------------------------------------------
| 1 | 1 | 4 |
--------------------------------------------------
| 2 | 1 | 10 |
--------------------------------------------------
| 3 | 2 | 11 |
--------------------------------------------------
| 4 | 2 | 15 |
--------------------------------------------------
I want to output:
--------------------------------------------------
| question_id | question | answers |
--------------------------------------------------
| 1 | What's 2+2? | 4,10 |
--------------------------------------------------
| 2 | What's 6+5? | 11,15 |
--------------------------------------------------
我正在尝试使用以下查询,但它没有返回任何内容:
SELECT
questions.question_id,
questions.question,
GROUP_CONCAT(answers.answer) as answers
FROM questions, answers
WHERE questions.exam_id = 1 AND answers.question_id = questions.question_id
GROUP BY questions.question_id
【问题讨论】:
标签: mysql