I'm trying to get only three rows from the joined table. For example, I have a categories table. I want to get popular categories and 3 articles from these categories without using SQL inside loop. Total post count should be 12.
I tried this but didn't work. (If I set limit 1
it works. But not working on limit 3
)
SELECT
categories.name AS cname,
categories.url AS curl,
articles.name,
articles.url,
articles.image
FROM
categories
JOIN articles ON articles.id = (
SELECT p.id
FROM articles AS p
WHERE categories.id = p.category AND p.delete = '0'
ORDER BY p.id DESC
LIMIT 3
)
WHERE
categories.delete = '0'
AND categories.popular = '1'