MySQL获取所有分类的前N条记录

 SELECT a1.* FROM t_content a1
INNER JOIN
(SELECT a.channelId,a.id FROM t_content a LEFT JOIN t_content b
<![CDATA[ ON a.channelId=b.channelId AND a.id<=b.id ]]>
AND a.status=2
GROUP BY a.channelId,a.id
<![CDATA[ HAVING COUNT(b.id)<=10]]>
)b1
ON a1.channelId=b1.channelId AND a1.id=b1.id
ORDER BY a1.channelId,a1.id DESC
原文地址:https://www.cnblogs.com/forthelichking/p/5806160.html