当前位置:数据库 > MySQL >>

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

 代码如下 复制代码
SELECT A1.* 
FROM Article AS A1 
     INNER JOIN (SELECT A.Category,A.InsertDate
                 FROM Article AS A 
                      LEFT JOIN Article AS B 
                        ON A.Category = B.Category 
                           AND A.InsertDate <= B.InsertDate 
                 GROUP BY A.Category,A.InsertDate
                 HAVING COUNT(B.InsertDate) <= @N
    ) AS B1 
    ON A1.Category = B1.Category 
       AND A1.InsertDate = B1.InsertDate
ORDER BY A1.Category,A1.InsertDate DESC
补充:数据库,mysql教程
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,