Kunne du prøve dette? Du kan teste her 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;
Nøgleideen er at generere samme seq
for samme på hinanden følgende gruppe som følger.
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
derefter grupperer du endelig GROUP BY grp, seq
som kan adskille hver på hinanden følgende grupper, selvom de har samme grp
.
EDIT:For at få præcis resultatet i eksemplet:
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