SELECT language,id,g
FROM (
SELECT language,id,
CASE WHEN [email protected]+1 THEN @n ELSE @n:[email protected]+1 END AS g,
@lastid := id As b
FROM
t, (SELECT @n:=0) r
ORDER BY
id
) s
REDIGER
Hvis du kun vil have 4 pr. gruppe, skal du tilføje en rækkenummervariabel:
SELECT language,id,g,rn
FROM (
SELECT language,id,
CASE WHEN [email protected]+1 THEN @n ELSE @n:[email protected]+1 END AS g,
@rn := IF(@lastid+1 = id, @rn + 1, 1) AS rn,
@lastid := id As dt
FROM
t, (SELECT @n:=0) r
ORDER BY
id
) s
Where rn <=4