2012-02-26 3 views
14

Ho bisogno di una funzione, che restituisca l'elenco delle stringhe.Funzione aggregata in MySQL - lista (come LISTAGG in Oracle)

Ho dati in tabella come questa:

Id MyString 
------------------------ 
1 First 
2 Second 
3 Third 
4 Fourth 

Ho bisogno funzione come questa (qualcosa di simile a questo funziona in Oracle):

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

che restituisce qualcosa di simile:

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

Qualche idea?

+4

Questo è ['GROUP_CONCAT()' in MySQL] (http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat). –

+0

possibile duplicato di [Risultati MySQL come lista separata da virgola] (http://stackoverflow.com/questions/662207/mysql-results-as-comma-separated-list) –

risposta

27

Siete alla ricerca di GROUP_CONCAT()

Prova questa:

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

Naturalmente, è possibile group by i risultati.