Potresti provare questo? Puoi testare qui 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;
L'idea chiave è generare lo stesso seq
per lo stesso gruppo consecutivo come 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
quindi infine raggruppando GROUP BY grp, seq
che possono differenziare ogni gruppo consecutivo anche se hanno lo stesso grp
.
EDIT:per ottenere esattamente il risultato nell'esempio:
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