Mysql – Aggregate function in MySQL – list (like LISTAGG in Oracle)


I need function, that returns list of strings.

I have data in table like this:

Id    MyString
 1    First
 2    Second
 3    Third
 4    Fourth

I need function like this (something like this works in oracle):

select LISTAGG(MyString, ', ') as myList where id < 4

That returns something like this:

First, Second, Third

Any ideas?

Best Solution

You're looking for GROUP_CONCAT()

Try this:

select group_concat(MyString separator ', ') as myList from table
where id < 4

Of course, you can group by the results.