开发者

Limit a query inside a JOIN?

开发者 https://www.devze.com 2023-02-12 15:53 出处:网络
I have a query which pulls out all articles and their categories (that matches the current category).

I have a query which pulls out all articles and their categories (that matches the current category).

I want it to fetch only 5 articles per category. how can i do it?

the query:

 SELECT a.* FROM
                    Articles   a
                    LEFT JOIN Articles_category b
                    ON a.id=b.article_id
                    LEFT JOIN Categories c
                    ON c.id = b开发者_运维百科.category_id
                    LEFT JOIN Articles_category ac1
                    ON ac1.main = 1 AND a.id = ac1.article_id AND ac1.position > 0
                    WHERE c.id = '14' 
                    ORDER BY b.main DESC

I want it to select only 5 articles tops for each category.


You can't do it with that query. You would have to loop a similar query to that, based on the category id. You could do it in a stored procedure, or loop the query repeatedly in code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号