Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a MySQL equivalent to PostgreSQL array_to_string

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?

like image 416
GoneAway Avatar asked Apr 10 '26 12:04

GoneAway


1 Answers

Try GROUP_CONCAT . e.g:

SELECT GROUP_CONCAT(name) AS names FROM people GROUP BY id;
like image 87
Chandu Avatar answered Apr 13 '26 15:04

Chandu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!