I’m trying to select the most recent entries per group in a table.
Say I have a table “blog_posts” which has a column for “id” (all unique, auto incremented), “post_cat” which can be values ‘category1’ or ‘category2’ or ‘category3’, and a “publish_status” column which can be values ‘online’ or ‘offline’.
How can I select the most recent entries for each category?
I have the following right now, but it almost feels like it’s selecting randomly:
select * FROM `blog_posts` WHERE (publish_status = 'online') GROUP BY post_cat ORDER BY id DESC LIMIT 10
I’d keep it real simple and use a trigger to maintain a last_post_id in the category table so you can easily join back on the posts table – something like this:
Simple Query
Tables
Triggers
Test Data
Hope this helps 🙂