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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:45:09+00:00 2026-05-14T19:45:09+00:00

How do I go about displaying the most recent post when I have two

  • 0

How do I go about displaying the most recent post when I have two tables, both containing a column called creation_date

This would be simple if all I had to do was get the most recent post based on posts created_on value however if a post contains replies I need to factor this into the equation.
If a post has a more recent reply I want to get the replies created_on value but also get the posts post_id and subject.

The posts table structure:

CREATE TABLE `posts` (
  `post_id` bigint(20) unsigned NOT NULL auto_increment,
  `cat_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `subject` tinytext NOT NULL,
  `comments` text NOT NULL,
  `created_on` datetime NOT NULL,
  `status` varchar(10) NOT NULL default 'INACTIVE',
  `private_post` varchar(10) NOT NULL default 'PUBLIC',
  `db_location` varchar(10) NOT NULL,
  PRIMARY KEY  (`post_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

The replies table structure:

CREATE TABLE `replies` (
  `reply_id` bigint(20) unsigned NOT NULL auto_increment,
  `post_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `comments` text NOT NULL,
  `created_on` datetime NOT NULL,
  `notify` varchar(5) NOT NULL default 'YES',
  `status` varchar(10) NOT NULL default 'INACTIVE',
  `db_location` varchar(10) NOT NULL,
  PRIMARY KEY  (`reply_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Here is my query so far. I’ve removed my attempt of extracting the dates.

$strQuery = "SELECT posts.post_id, posts.created_on, replies.created_on, posts.subject ";
$strQuery = $strQuery."FROM posts ,replies ";
$strQuery = $strQuery."WHERE posts.post_id = replies.post_id ";
$strQuery = $strQuery."AND posts.cat_id = '".$row->cat_id."'";
  • 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-14T19:45:10+00:00Added an answer on May 14, 2026 at 7:45 pm
    $strQuery = "
      SELECT posts.post_id, GREATEST(posts.created_on, replies.created_on)  AS latestDate, posts.subject
      FROM posts, replies
      WHERE posts.post_id = replies.post_id
      AND posts.cat_id = {$row->cat_id}
      GROUP BY posts.post_id
      ORDER BY latestDate DESC;
    ";
    

    UPDATE: On second looks, the above is actually incorrect, as it will not include those posts which do not yet have any replies. The more correct way to do it is:

    $strQuery = "
      SELECT posts.post_id, GREATEST(posts.created_on, replies.created_on) AS latestDate,
      FROM posts
      LEFT JOIN replies ON (posts.post_id = replies.post_id)
      WHERE posts.cat_id = {$row->cat_id}
      GROUP BY posts.post_id
      ORDER BY latestDate DESC
      LIMIT 0,1;
    ";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer IMO counting lines of code is a terrible quality metric.… May 14, 2026 at 10:29 pm
  • Editorial Team
    Editorial Team added an answer That's not blurred text, it's called a "watermark." You can… May 14, 2026 at 10:29 pm
  • Editorial Team
    Editorial Team added an answer In my opinion, a Rich Internet Application only makes sense… May 14, 2026 at 10:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.