Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to find the MySQL equivalent of the PostgreSQL functions array and array_to_string and came across this post but asking for oracle9i which doesn't help me. I need to achieve this with MySQL but even Google can't seem to find any suitable answers.

So you don't have to read two posts, here is a repeat of the question:

In PostgreSQL, using the array and array_to_string functions can do the following:

Given the table "people":

id | name
---------
1  | bob
2  | alice
3  | jon

The SQL:

SELECT array_to_string(array(SELECT name FROM people), ',') AS names;

Will return:

names
-------------
bob,alice,jon

Anyone have any ideas how to achieve this in MySQL?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Try GROUP_CONCAT . e.g:

SELECT GROUP_CONCAT(name) AS names FROM people GROUP BY id;
share|improve this answer
    
@DangerPaws, See: dev.mysql.com/doc/refman/5.5/en/… for more details. –  Johan May 28 '11 at 15:01
    
Excellent! Thanks for that! Exactly what I needed :) @Johan Thanks for the link –  DangerPaws May 28 '11 at 15:13
    
I take it from the documentation that I can't put a SELECT statement inside the GROUP_CONCAT function? –  DangerPaws May 28 '11 at 15:24
1  
@myself, correction you can put a SELECT statement inside the GROUP_CONCAT but need to wrap the SELECT statement in an extra pair of brackets like this: GROUP_CONCAT((SELECT...)) –  DangerPaws May 28 '11 at 15:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.