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

  • Home
  • SEARCH
  • 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 6663225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:28:33+00:00 2026-05-26T02:28:33+00:00

On my database, I have to show a preview with all the posts with

  • 0

On my database, I have to show a preview with all the posts with their last content and I don’t really know how to do it.

CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL auto_increment,
`post_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`body` text NOT NULL,
`created` date NOT NULL,
PRIMARY KEY  (`id`),
KEY `post_id` (`post_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

INSERT INTO `posts` (`id`, `post_id`, `title`, `body`, `created`) VALUES 
(1, 1, 'hello world', 'this is the first post', '2011-10-10'), 
(2, 2, 'the second post', 'and this is the second post', '2011-10-10'), 
(3, 2, 'second post', 'title is modified, this is a better version', '2011-10-11');

So the result has to be the post with id 1 and 3. The second post, appears twice because there are two versions on different dates. The dates of the posts marks which version is newer.

I need to display the last state of all the post. How can I do that?

  • 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-26T02:28:33+00:00Added an answer on May 26, 2026 at 2:28 am

    This is a Top-N Per Group Problem:

    Here is how you would do it:

    SELECT A.* FROM (
        SELECT 
            p.*,
            IF( @p <> ID, @n := 1, @n := @n+1 ) AS r, 
            @p := post_id
        FROM
            posts p
        ORDER BY post_id, created DESC
        ) AS A
    WHERE r = 1
    

    For more information:
    http://www.artfulsoftware.com/infotree/queries.php#104

    Explanation:

    SELECT A.* FROM (
        SELECT 
            p.*,
            IF( 
                    @p <> ID,   /* check if this record is a new post_id */
                    @n := 1,    /* if it is a new post_id, reset the counter to 1 */
                    @n := @n+1  /* other wise, increment the counter */
                ) AS r,
            @p := post_id   /* assign the post_id of this record to @p to use in the IF statement */
        FROM
            posts p
        ORDER BY 
            post_id,     /*first, order by postID so we scan over all versions of posts together */
            created DESC /* then order by createDate DESC so the newest versions of each post are first */
        ) AS A
    WHERE r = 1
    

    Sample Results (of the subquery):

    id | post_id | title | body | created    | r | @p
    1  | 1       | ...   | ...  | 10/10/2011 | 1 | 1
    3  | 2       | ...   | ...  | 10/11/2011 | 1 | 2
    2  | 2       | ...   | ...  | 10/10/2011 | 2 | 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to list out all records from a database that have not
I have a checkbox in GridViewColumn which i use for show/change database value. The
I have a background worker running a long database task. i want to show
I have a bunch of old classic ASP pages, many of which show database
I have database with many tables. In the first table, I have a field
i have database table like this +-------+--------------+----------+ | id | ip | date |
So I have dynamically generated content from a MySQL database and I pull it
My database have got 4 table : users id username images id user_id image
I have database table Inbox(id,accountid,emailfrom,emailsubject,emailbody, recieveddate,attachment, read(bool) i have added it to the form
Basically what I need to know is this: I have to show a drop

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.