0

我在下面有一个查询,它显示了这些结果:

SELECT q.QuestionId, q.QuestionContent, an.Answer
FROM Answer an
INNER JOIN Question q ON q.AnswerId = an.AnswerId;

查询结果:

QuestionId     QuestionContent           Answer
1              Who are me and you         B
1              Who are me and you         D
2              Name these Cars            A
2              Name these Cars            B
2              Name these Cars            E
3              What is 2+2                B

我想要做的是将相同 QuestionId 的答案合并在一起,因此结果如下所示:

QuestionId     QuestionContent           Answer
1              Who are me and you         B D
2              Name these Cars            A B E
3              What is 2+2                B

这可能吗?

谢谢

4

1 回答 1

1

试试这个 -

SELECT q.QuestionId,
       q.QuestionContent,
       GROUP_CONCAT(an.Answer, SEPARATOR ' ')
FROM Answer an
INNER JOIN Question q ON q.AnswerId = an.AnswerId
GROUP BY q.QuestionId,
       q.QuestionContent 
于 2012-05-25T21:33:54.340 回答