0

我有下表:

surveys
comp_id    question
4          What is your name?
4          How are you?
4          Where do you live?
5          Who are you?
5          What is your birthday?

我需要帮助编写一个查询,它给我以下输出:

comp_id    my_questions
4          What is your name?How are you?Where do you live?
5          Who are you?What is your birthday?

谢谢,

4

1 回答 1

2

您正在寻找该GROUP_CONCAT()功能。像这样使用它:

SELECT comp_id, GROUP_CONCAT([DISTINCT] question [ORDER BY some_field ASC/DESC] [SEPARATOR '']) as my_questions
FROM surveys
GROUP BY comp_id

注意我已经展示了一些可选的值来传递到 GROUP_CONCAT 中[]。要获得与您展示的完全一样的信息,请使用GROUP_CONCAT(question SEPARATOR ''). 可选项目可让您查找不同的问题值或按任何字段(包括问题本身)对它们进行排序。

于 2012-12-13T20:49:37.210 回答