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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:05:13+00:00 2026-05-16T21:05:13+00:00

Hey guys, quick question. I have a table of messages. There are two types

  • 0

Hey guys, quick question. I have a table of messages.

There are two types of messages:

  • Messages with a unique group id
  • autonomous messages

Messages with the same group id as at least one other message – these I want to group together when they are presented. The grouped messages are messages with replies to them. What I want to do is create a query in order to put the grouped messages together in the place where the earliest/root message of that group would be in relation chronologically with the rest of the messages regardless of the time the replies are entered.

my current query is this, and obviously only sorts messages chronologically.

  SELECT timestamp, 
         user, 
         message, 
         group_id 
    FROM messages 
   WHERE topic_id = ? 
ORDER BY timestamp DESC 
   LIMIT 10

Does anyone have any suggestions?

  • 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-16T21:05:14+00:00Added an answer on May 16, 2026 at 9:05 pm

    What about this?

    SELECT m.timestamp, m.user, m.message, m.group_id, g.grp_timestamp
      FROM messages AS m JOIN
           (SELECT group_id, MIN(timestamp) AS grp_timestamp
              FROM messages
             GROUP BY group_id) AS g ON m.group_id = g.group_id
     WHERE m.topic_id = ?
     ORDER BY g.grp_timestamp, g.group_id, m.timestamp;
    

    The logic is to identify the earliest timestamp for each group_id in the sub-query (presuming messages with no responses have a valid group_id assigned) and then sort first by the group timestamp, then by group ID (so that if two groups end up with the same timestamp somehow, the messages are still sorted correctly), and then by the message timestamp.

    You might want to push the WHERE clause into the sub-select too.


    If you want the most recent messages first, then basically, you apply DESC to the ORDER BY clause in the appropriate places.

    SELECT m.timestamp, m.user, m.message, m.group_id, g.grp_timestamp
      FROM messages AS m JOIN
           (SELECT group_id, MIN(timestamp) AS grp_timestamp
              FROM messages
             GROUP BY group_id) AS g ON m.group_id = g.group_id
     WHERE m.topic_id = ?
     ORDER BY g.grp_timestamp DESC, g.group_id, m.timestamp DESC;
    

    You might want the group with the most recent response to appear first; in that case, you need to adjust the aggregate from MIN to MAX:

    SELECT m.timestamp, m.user, m.message, m.group_id, g.grp_timestamp
      FROM messages AS m JOIN
           (SELECT group_id, MAX(timestamp) AS grp_timestamp
              FROM messages
             GROUP BY group_id) AS g ON m.group_id = g.group_id
     WHERE m.topic_id = ?
     ORDER BY g.grp_timestamp DESC, g.group_id, m.timestamp;
    

    As long as you are OK with the responses appearing in chronological order, I think that does the trick. If the responses must appear in reverse chronological order with the original message appearing first, you have some difficulties. If you tagged the messages with ‘L’ for ‘leader’ and ‘F’ for ‘follower’ in the messages table, then you can use that column to do the sorting. If you have to dynamically determine whether a message is a leader or follower, you have to work harder.

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

Sidebar

Related Questions

Hey guys, one more quick question for any experts out there. I have a
Hey guys quick question, I have an entry that I put in my database,
Hey guys quick question, I currently have an insert statement $query= INSERT into new_mail
Hey guys quick question. I have a div that gets assigned a number to
Hey guys, quick question for any experts out there. I am allowing users to
Hey guys quick question, I have an add remove script that will add a
Hey guys really quick question, I have a simple test that I was doing
Hey guys I have a quick question, I have a dialog box that has
Hey guys, quick question, I have a link when clicked drops down a list.
Hey guys, quick question, I have an id that is applied multiple times on

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.