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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:51:59+00:00 2026-05-26T17:51:59+00:00

I have two tables Posts and comments Post Table Post_id Post_content Comments table comment_id

  • 0

I have two tables Posts and comments

Post Table

Post_id
Post_content

Comments table

comment_id
Comment
post_id
created_date

One post can have multiple comments or zero comments

My requirement is getting the latest comment for the posts using left outer join .

I mean result should be one record for the post with the below columns .

post_id,post_content ,comment_id,comment 

In simple words posts should be getting along with their latest comment if it exists.

( Currently the system is getting the posts first and then going to the server again to get the latest comments to display , thought of getting them in one shot since we are displaying only one comment initially … Not sure what should be the best approach if wants to display more than one comment ..?)

Thank You

Regards

Kiran

  • 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-26T17:51:59+00:00Added an answer on May 26, 2026 at 5:51 pm
    SELECT Post.post_id, post_content, comment_id, comment
    FROM
        Post LEFT JOIN Comments
            ON Post.post_id = Comments.post_id
            AND created_date = (
                SELECT MAX(created_date)
                FROM Comments
                WHERE Post.post_id = Comments.post_id
            )
    

    BTW, you should consider indexing Comments {post_id, created_date} for optimal performance, but be mindful of the non-primary key index overhead in case you are using InnoDB (see the “Disadvantages of clustering” section in this article).

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

Sidebar

Related Questions

I have two tables posts and comments . Table comments have post_id attribute. I
Suppose we have two tables. Post and Comment. Post has many Comments. Pretend they
I have two tables called 'events' and 'topics' each table can have many comments.
I have two tables as posts and comments. Each row in comments table has
Suppose I have two tables, one with blog posts and another with readers and
I have a table of comments and a table of posts Whenever a post
I have two MySQL (MyISAM) tables: Posts: PostID(primary key), post_text, post_date, etc. Comments: CommentID(primary
I have two models, one called Notes and one called Comments. Comments can be
I have two tables sturctured like so: Posts Table id content Meta Table id
I have two tables, one that contains volunteers, and one that contains venues. Volunteers

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.