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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:37:23+00:00 2026-05-29T12:37:23+00:00

In MySql I have table: idPost | idPostParent | postText | dateCreated | 1

  • 0

In MySql I have table:

idPost | idPostParent | postText                              | dateCreated         |
1      | 1            |  This is parent post no1              | 2012-01-01 12:00:00 |
2      | 2            |  This is parent post no2              | 2012-01-02 13:00:00 |
3      | 1            |  This is first reply on idPost:1      | 2012-01-02 13:30:00 | 
4      | 4            |  This is parent post no3              | 2012-01-04 10:00:00 |
5      | 2            |  This is first reply on idPost:2      | 2012-01-04 11:00:00 |
6      | 1            |  This is second reply on idPost:1     | 2012-01-05 15:00:00 |
7      | 2            |  This is second reply on idPost:2     | 2012-01-06 17:00:00 |

NOTE:

  1. PARENT post is detected if idPost = idPostParent
  2. REPLY post is detedted if idPost <> idPostParent and reply is
    related to post writen in idPostParent

I need SQL query for sorting and grouping record sets in table by some rules:

  1. Posts must be grouped by idPostParent (parent post first then its
    replies)
  2. The PARENT post that is latest entered OR have latest entered reply must be on the top.

The resultset should be:

idPost | idPostParent | postText                              | dateCreated         |
2      | 2            |  This is parent post no2              | 2012-01-02 13:00:00 |
5      | 2            |  This is first reply on idPost:2      | 2012-01-04 11:00:00 |
7      | 2            |  This is second reply on idPost:2     | 2012-01-06 17:00:00 |
1      | 1            |  This is parent post no1              | 2012-01-01 12:00:00 |
3      | 1            |  This is first reply on idPost:1      | 2012-01-02 13:30:00 |
6      | 1            |  This is second reply on idPost:1     | 2012-01-05 15:00:00 |
4      | 4            |  This is parent post no3              | 2012-01-04 10:00:00 |

REASON:
The PARENT post idPost=2 has latest entered reply post so its thread must be on the top of the table.

Thanks to anyone who may contribute to the solution!

  • 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-29T12:37:24+00:00Added an answer on May 29, 2026 at 12:37 pm

    Here’s one way:

    SELECT p.*
    FROM posts p
    LEFT JOIN
     (SELECT idPostParent, MAX(dateCreated) AS time_of_newest
      FROM posts
      GROUP BY idPostParent) p2
     ON p.idPostParent = p2.idPostParent
    ORDER BY time_of_newest DESC, idPost=p.idPostParent DESC, dateCreated DESC
    

    Explanation:

    The sub-query

    SELECT idPostParent, MAX(dateCreated) AS time_of_newest
      FROM posts
      GROUP BY idPostParent
    

    selects the maximum date created per “thread” (idPostParent).

    This is LEFT JOINd to the main posts table, so that every thread has a time_of_newest for that thread.

    We sort by time_of_newest DESCENDING (this puts thread 2 in front of thread 1 in front of thread 4).

    Then, we sort by idPost=idPostParent1 DESC in order to get parent posts in front of replies (parent posts have a 1 here and replies all have a 0, and since we sort descending parent posts go first).

    Finally, we sort by dateCreated DESC to get the replies in order.

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

Sidebar

Related Questions

I have MySQL Table with an Order table in a DB this table has
I would like to ask something about query using mysql I have this table
Using MySQL 5 I have a table like this: date (varchar) door (varchar) shift
I have mysql table with data like this. record will have server with total
I have MySQL table that doesn't have AUTO_INCREMENT field. In PHP I run this
i work with php/Mysql i have table with two cols (date , cash) with
I have mysql table that has a column that stores xml as a string.
In MySql I could have a table with an auto increment column and insert
I am using MySQL and I have a table with an index that is
I have a table in MySQL that has 3 fields and I want to

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.