2010-09-28 18 views
21

Ho una query che restituisce valori ma ho bisogno di essi come uscita singola separata da virgole.concat l'output della sottoquery?

Quindi ho provato a concat l'output con la virgola ma non ha funzionato?

select id from videos where duration=0; /// this would return some rows 

Ho provato concat e concat_ws ma non ha funzionato

select concat(select concat(id,',') from videos where duration=0); 
select concat((select id from videos where duration=0),','); 
select concat_ws(',',(select id from videos where duration=0)); 

ho bisogno gli ID di tutte le righe con la virgola separtor

per esempio l'uscita dovrebbe essere 1,4,6 , 78.565

qualche idea?

+4

Se si utilizza 'GROUP_CONCAT' tenere a mente è limitato a 1024. Quindi, se la stringa viene tagliato fuori, alzare il limite (tendo a ignorarlo di tanto in tanto;)) – DrColossos

+0

Questo è esattamente quello che è successo quando l'ho provato ieri. La mia query restituirebbe più di 2500 valori .. Come posso aumentare il limite? – Vijay

risposta

29

Questo è ciò che fa group_concat.

select group_concat(id) as video_list 
from videos 
where duration=0 
5

Uso group_concat:

Questa funzione restituisce un risultato stringa con i valori non NULL concatenati da un gruppo. Restituisce NULL se non ci sono valori non NULL.

SELECT 
    GROUP_CONCAT(id) 
FROM 
    videos 
WHERE 
    duration=0