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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:46:35+00:00 2026-06-13T17:46:35+00:00

This a simplified version of my SQL table for questions and answers, post_id is

  • 0

This a simplified version of my SQL table for questions and answers, post_id is the primary key.

 post_id | ref_post_id | title  | date (unix timestamp)
   1     |     0       | Title1 |  10 
   2     |     1       |        |  20 
   3     |     0       | Title2 |  30 

When ref_post_id is 0, that means it’s a question. When it is not 0 it’s an answer where the number corresponds to the question’s post_id. That means that the second row is an answer to the first row in my example above.

What I want to do is to get only the last row of every question. If the question does not have any answers I want the question row, but if it has answers I want the last answer but I want to join in the title that belongs to the question row.

I’ve googled and tried myself for a while now. Hopefully you understand my problem!

Thanks!

  • 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-06-13T17:46:37+00:00Added an answer on June 13, 2026 at 5:46 pm

    I would reccommend to split your table in two different tables, one for the questions and one for the answer.

    However, the query you need could be written as this:

    SELECT posts.*
    FROM
      posts INNER JOIN
        (SELECT IF(ref_post_id=0,post_id,ref_post_id) as id, Max(Date) as maxdate
         FROM posts
         GROUP BY id) p
      ON
        (p.maxdate = posts.date) AND
        (p.id =IF(posts.ref_post_id=0,posts.post_id,posts.ref_post_id))
    

    To select max date for every question/answer you have to group by ref_post_id, except the case in which ref_post_id = 0 where you have to group by post_id, hence the IF.

    When you join the posts table with itself, using the subquery, you have to use the same trick.

    EDIT: if you need to mark a question as deleted, then you have to remove all deleted questions and answers from the select above, and you have to use one more subquery to select only undeleted questions:

    SELECT posts.*
    FROM
      posts INNER JOIN
        (SELECT IF(ref_post_id=0,post_id,ref_post_id) as id, Max(Date) as maxdate
         FROM posts
         WHERE EXISTS (SELECT null 
                       FROM posts posts_sub
                       WHERE
                         posts_sub.post_id =
                         IF(posts.ref_post_id=0,posts.post_id,posts.ref_post_id)
                       AND posts_sub.deleted =0)
         GROUP BY id) p
      ON
        (p.maxdate = posts.date) AND
        (p.id =IF(posts.ref_post_id=0,posts.post_id,posts.ref_post_id))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
I have this table for documents (simplified version here): id rev content 1 1
We have a table like this (simplified version): Items: Itemid Itemname Itemfatherid itemA theitemA
Hello guys this is a very simplified version of my table: I will like
This is a simplified version of the problem I am trying to solve: There
This is a simplified version of my question earlier today since I didn't get
So this is the simplified version of the data file: Wichita, KS[3769,9734]279835 308 1002
So this is the simplified version of the data file: Wichita, KS[3769,9734]279835 308 1002
This is some code that is behaving peculiarly. This is a simplified version of
I am not able to link RtlIpv4StringToAddressExW(). This is a simplified version of my

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.