MySQL select from the same table but from different category with the same limit

You need subqueries:

(SELECT * FROM makale WHERE category = 'Histoire' LIMIT 12)
UNION ALL
(SELECT * FROM makale WHERE category = 'Cultures' LIMIT 12)
UNION ALL
(SELECT * FROM makale WHERE category = 'Sujet Divers' LIMIT 12)
ORDER BY id DESC;

You can also write this using window functions, but that is not your question.

Note: This returns an arbitrary 12 rows for each category. I’m leaving the code as is, because this answers the question you have asked. But if you want a particular set of 12 rows, then you should use an ORDER BY in each subquery.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top