Reputation: 4393
I wonder if there's a counterpart of array_agg()
and array_to_string
of PostgreSQL
in MySQL
(Or anything close to this function at least) I haven't had any luck on my research trying to find something similar to this two. For non-PostgreSQL users, here's how it works.
Normally, executing:
SELECT my_column FROM my_table;
Will result to:
--------------
| my_column |
|------------|
| DATA1 |
| DATA2 |
| DATA3 |
| DATA4 |
--------------
On the other hand, executing the statement:
SELECT array_agg(my_column) FROM my_table;
Will result to:
A single row with all the data from my_column.
-----------------------------------
| array_agg(my_column) |
|---------------------------------|
|{"DATA1","DATA2","DATA3","DATA4"}|
-----------------------------------
And array_to_string, as the function name says, it converts the array into a single String.
Converting the array_agg(my_column) will return the following:
-------------------------
| |
|-----------------------|
|DATA1,DATA2,DATA3,DATA4|
-------------------------
Depending on your separator. In my case, it's comma.
*******************Edit(SOLUTION)*******************
Solution (Written by IgorRomanchenko):
SELECT GROUP_CONCAT(my_column SEPARATOR ',');
Upvotes: 2
Views: 7601
Reputation: 28561
MySQL does not have bult in array type, but you can use string aggregation (like GROUP_CONCAT
) to aggregate multiple values to a single string.
Upvotes: 7