SELECT item_id, item_name, catid FROM ( SELECT item_id, item_name, catid, CASE WHEN @catid != catid THEN (@rownum := 1) + !(@catid := catid) ELSE @rownum := $rownum + 1 AS rownum FROM ( SELECT item_id, item_name, i.catid FROM items AS i INNER JOIN categories AS c ON i.catid = c.catid ORDER BY i.catid ASC) AS h, (SELECT @catid := NULL, @rownum := NULL) AS var HAVING rownum <= 5 LIMIT 20) AS h2 ORDER BY item_id
Devart's solution looks right, but I'm a little pickled. Mine cannot do much more attention for others, but it is with me.
EDIT: Understand the Devart solution now, and actually it's pretty brilliant. :)
Robin castlin
source share