MySQL Query SELECT多行

时间:2012-12-13 20:45:33

标签: mysql select group-concat

我有下表:

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?

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

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

谢谢,

1 个答案:

答案 0 :(得分: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 '')。可选项允许您查找不同的问题值或按任何字段(包括问题本身)对它们进行排序。