3

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?

1 Answer 1

8

Try GROUP_CONCAT . e.g:

SELECT GROUP_CONCAT(name) AS names FROM people GROUP BY id;
Sign up to request clarification or add additional context in comments.

4 Comments

@DangerPaws, See: dev.mysql.com/doc/refman/5.5/en/… for more details.
Excellent! Thanks for that! Exactly what I needed :) @Johan Thanks for the link
I take it from the documentation that I can't put a SELECT statement inside the GROUP_CONCAT function?
@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...))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.