Você poderia tentar isso? Você pode testar aqui http://www.sqlfiddle.com/#!2/57967 /12 .
Select grp_new, group_concat(ord)
From (
Select ord, if(grp = @prev, @seq, @seq := @seq + 1) as seq,
if(grp = @prev, grp, @prev := grp) as grp_new
From tab, (SELECT @seq := 0, @prev := '') AS init
Order by ord
) x
Group by grp_new, seq;
A ideia chave é gerar o mesmo
seq
para o mesmo grupo consecutivo como segue. Select
ord, if(grp = @prev, @seq, @seq := @seq + 1) as seq,
if(grp = @prev, grp, @prev := grp) as grp_new
From tab, (SELECT @seq := 0, @prev := '') AS init
Order by ord
então finalmente agrupando
GROUP BY grp, seq
que pode diferenciar cada grupo consecutivo mesmo se eles tiverem o mesmo grp
. EDIT:Para obter exatamente o resultado no exemplo:
Select grp_new, group_concat(ord order by ord)
From (
Select ord, if(grp = @prev, @seq, @seq := @seq + 1) as seq,
if(grp = @prev, grp, @prev := grp) as grp_new
From tab, (SELECT @seq := 0, @prev := '') AS init
Order by ord
) x
Group by seq