Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

myList ------------------------ First, Second, Third 

Any ideas?

like image 705
Kamil Avatar asked Feb 26 '12 19:02

Kamil


People also ask

What is alternative for Listagg in Oracle?

In order to concatenate field values, I would use “GROUP_CONCAT” function in Virtual DataPort Administration tool which is similar to LISTAGG function. For example, GROUP_CONCAT('<row separator>',<field_name>)

What is list aggregate function in Oracle?

An Oracle LISTAGG Function is an aggregate function that returns a single row. This is used to transform data from multiple rows into a single list of values separated by a given delimiter. It operates on all rows and returns single. It returns a comma or other delimiter separatedresult set just like an excel CSV file.

Is Listagg an aggregate function?

The LISTAGG function is used to aggregate a set of string values within a group into a single string by appending the string-expression values based on the order that's specified in the 'WITHIN GROUP' clause. As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.

Can we use Listagg in Oracle Forms?

You cannot use LISTAGG function directly in forms. Instead you can create a record group, which uses the LSTAGG and use the record group.


1 Answers

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.

like image 138
Mosty Mostacho Avatar answered Oct 14 '22 18:10

Mosty Mostacho