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 3279276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:34:27+00:00 2026-05-17T19:34:27+00:00

I am trying to get the latest modified posts, from a group of posts,

  • 0

I am trying to get the latest modified “posts”, from a group of “posts”, that all have the same parent_id. I already learned today that grouping is not the answer, and that I should either use an inner subquery, or a “within-group aggregate”, thanks to this site >> Aggregates >> Within-group aggregates. I chose to go with the latest, as I find it nicer, easier to implement given that I use CakePHP, and is said to be faster (can you confirm?)

So I came up with the following query:

SELECT `Post`.`id`, `Post`.`parent_id`, `Post`.`modified` 
FROM `cake_posts` AS `Post` 
LEFT JOIN `cake_posts` AS `SelfPost` ON
      (`Post`.`id` = `SelfPost`.`id` AND `Post`.`modified` < `SelfPost`.`modified`)
WHERE `SelfPost`.`id` IS NULL 
ORDER BY `Post`.`parent_id` ASC

But here is the result:

|-----------------------------------|
| id |parent_id|      modified      |
|-----------------------------------|
| 1  |    1    |2010-10-16 17:04:43 |
| 3  |    1    |2010-10-16 20:04:53 |
| 6  |    2    |2010-10-16 21:46:59 |
| 5  |    2    |2010-10-16 21:46:44 |
| 2  |    2    |2010-10-16 17:06:10 |
| 4  |    4    |2010-10-16 21:46:19 |
| 7  |    7    |2010-10-16 22:03:19 |
| 8  |    8    |2010-10-16 22:03:25 |
| 9  |    9    |2010-10-16 22:03:32 |
| 10 |    10   |2010-10-17 00:18:10 |
| 11 |    11   |2010-10-17 00:18:15 |

And as you can see, I still have duplicates parent_id, so what am I doing wrong?

EDIT: Basically it is a forum only very light, were topics and posts are stored in the same table. So each post (aka message, aka row in the table) either has a different parent, or himself as a parent (but I don’t think this is relevant at this point). I am trying to build the index, so for each topic (ie posts with the same parent_id) I want only the latest modified message. What I would like to get is:

|-----------------------------------|
| id |parent_id|      modified      |
|-----------------------------------|
|    |    -    |                    |
| 3  |    1    |2010-10-16 20:04:53 |
| 6  |    2    |2010-10-16 21:46:59 |
|    |    -    |                    |
|    |    -    |                    |
| 4  |    4    |2010-10-16 21:46:19 |
| 7  |    7    |2010-10-16 22:03:19 |
| 8  |    8    |2010-10-16 22:03:25 |
| 9  |    9    |2010-10-16 22:03:32 |
| 10 |    10   |2010-10-17 00:18:10 |
| 11 |    11   |2010-10-17 00:18:15 |

Further data:

CREATE TABLE IF NOT EXISTS `cake_posts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) unsigned NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`, `parent_id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='Posts to topics' AUTO_INCREMENT=12 ;

INSERT INTO `cake_posts` (`id`, `parent_id`, `modified`) VALUES
(1, 1, '2010-10-16 17:04:43'),
(3, 1, '2010-10-16 20:04:53'),
(6, 2, '2010-10-16 21:46:59'),
(5, 2, '2010-10-16 21:46:44'),
(2, 2, '2010-10-16 17:06:10'),
(4, 4, '2010-10-16 21:46:19'),
(7, 7, '2010-10-16 22:03:19'),
(8, 8, '2010-10-16 22:03:25'),
(9, 9, '2010-10-16 22:03:32'),
(10, 10, '2010-10-17 00:18:10'),
(11, 11, '2010-10-17 00:18:15');
  • 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-17T19:34:28+00:00Added an answer on May 17, 2026 at 7:34 pm

    Well, after much trying and frustration, I finally figured out what was going wrong. A simple mistake actually, I was joining on the wrong field. Because, since I wanted to group By parent_id initially, I should have realized that it was also by parent_id that I had to join, and not by id. So here is the corrected query:

    SELECT `Post`.`id`, `Post`.`parent_id`, `Post`.`modified` 
    FROM `cake_posts` AS `Post` 
    LEFT JOIN `cake_posts` AS `SelfPost` ON
          (`Post`.`parent_id` = `SelfPost`.`parent_id` AND `Post`.`modified` < `SelfPost`.`modified`)
    WHERE `SelfPost`.`id` IS NULL 
    ORDER BY `Post`.`modified ` DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the latest revision ID from my SVN project using Phing.
I get the following error when trying to run the latest Cygwin version of
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Im trying to get a completly data copy from a gridview, itryed clone(), tryed
Hey guys I've been trying all day to get an ant file to automatically
I am trying to get the latest record inserted in a SQL table based
I was trying to get the latest ruby on rails for my Mac running
I am trying to get a source code for an application from github. But
I'm trying to set up MinGW. I have downloaded the latest installer, which installs

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.