Mysql查询从每个类别获取前5个子类别

时间:2011-07-08 04:48:37

标签: mysql

我需要一个查询;标准是:

我想从每个类别中仅选择最新的5个子类别;

当前查询是:

SELECT a.id,a.catagory_name,b.id as sucid,b.subcatagory_name
FROM category a
LEFT JOIN subcategory b ON a.id = b.catagory_id
WHERE a.active='y' AND b.active='y'
ORDER BY a.priority,b.subcatagory_name

2 个答案:

答案 0 :(得分:0)

请试试这个。

SELECT a.id,a.catagory_name,b.id as sucid,b.subcatagory_name
FROM category a
LEFT JOIN subcategory b ON a.id = b.catagory_id
WHERE a.active='y' AND b.active='y'
ORDER BY a.priority,b.category_id DESC
LIMIT 0,5

这可能适合你。

答案 1 :(得分:0)

让我们试试这个:

SELECT a.id,a.catagory_name,B.id as sucid,B.subcatagory_name
FROM category a 
(SELECT id,subcatagory_name FROM subcategory WHERE catagory_id = a.id ORDER BY id DESC LIMIT 5) AS B
ON a.id = B.catagory_id
ORDER BY a.priority,B.category_id;