Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3987448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:08:04+00:00 2026-05-20T06:08:04+00:00

I’m trying to select the most recent entries per group in a table. Say

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:08:05+00:00Added an answer on May 20, 2026 at 6:08 am

    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

    select
     pc.cat_id,
     pc.name,
     u.username,
     bp.*
    from
     post_category pc
    inner join blog_post bp on pc.last_post_id = bp.post_id
    inner join users u on bp.user_id = u.user_id
    order by
     pc.cat_id;
    
    +--------+------+----------+---------+---------+---------------------+
    | cat_id | name | username | post_id | user_id | post_date           |
    +--------+------+----------+---------+---------+---------------------+
    |      1 | cat1 | bar      |       3 |       2 | 2011-02-09 12:45:33 |
    |      2 | cat2 | BAR      |       5 |       3 | 2011-02-09 12:45:33 |
    |      3 | cat3 | f00      |       4 |       1 | 2011-02-09 12:45:33 |
    +--------+------+----------+---------+---------+---------------------+
    

    Tables

    drop table if exists post_category;
    create table post_category
    (
    cat_id smallint unsigned not null auto_increment primary key,
    name varchar(255) unique not null,
    last_post_id int unsigned null,
    key (last_post_id)
    )
    engine=innodb;
    
    drop table if exists users;
    create table users
    (
    user_id int unsigned not null auto_increment primary key,
    username varbinary(32) unique not null
    )
    engine=innodb;
    
    drop table if exists blog_post;
    create table blog_post
    (
    post_id int unsigned not null auto_increment primary key,
    user_id int unsigned not null,
    post_date datetime not null,
    key (post_date, user_id)
    )
    engine=innodb;
    
    drop table if exists blog_post_category;
    create table blog_post_category
    (
    cat_id smallint unsigned not null,
    post_id int unsigned not null,
    primary key (cat_id, post_id)
    )
    engine=innodb;
    

    Triggers

    delimiter #
    
    create trigger blog_post_before_ins_trig before insert on blog_post
    for each row
    begin
      set new.post_date = now();
    end#
    
    create trigger blog_post_category_before_ins_trig before insert on blog_post_category
    for each row
    begin
      update post_category set last_post_id = new.post_id where cat_id = new.cat_id;
    end#
    
    delimiter ;
    

    Test Data

    insert into post_category (name) values ('cat1'),('cat2'),('cat3'),('cat4');
    insert into users (username) values ('f00'),('bar'),('BAR'),('alpha'),('beta');
    
    insert into blog_post (user_id) values (1),(1),(2),(1),(3);
    insert into blog_post_category (cat_id, post_id) values
    (1,1),(1,3),
    (2,1),(2,5),
    (3,1),(3,3),(3,4);
    

    Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.