SET @a:=0; SET @cid:=0;
SELECT
`q`.*
FROM (SELECT
`contents`.*
FROM `contents`
ORDER BY `cid` ASC, `id` DESC
) AS `q`
WHERE (IF (@cid=`q`.`cid`, @a:=@a+1, (@cid:=`q`.`cid`) AND (@a:=1)) <= 5);
* This source code was highlighted with Source Code Highlighter.