mysql group by 并查出前面几条
select a.*,
b.name as bname
from sk_product a
left join sk_product_category b
on a.category_id = b.id
where 6 > (
select count(*)
from sk_product
and order_list > a.order_list
)
order by a.category_id
本文介绍了一种使用MySQL进行分组查询并返回每个分组前几条记录的方法。通过一个具体的SQL语句示例,展示了如何结合左连接及子查询实现这一需求。此技巧对于需要展示各分类下热门产品等场景非常实用。
select a.*,
b.name as bname
from sk_product a
left join sk_product_category b
on a.category_id = b.id
where 6 > (
select count(*)
from sk_product
and order_list > a.order_list
)
order by a.category_id
2938

被折叠的 条评论
为什么被折叠?