WEBProject
WEBProject

Reputation: 1345

Limit a query inside a JOIN?

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.

Upvotes: 0

Views: 153

Answers (1)

corrodedmonkee
corrodedmonkee

Reputation: 373

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.

Upvotes: 1

Related Questions