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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:09:50+00:00 2026-06-01T20:09:50+00:00

My site has a messaging feature where one user may message another. The messages

  • 0

My site has a messaging feature where one user may message another. The messages support threading – a parent message may have any number of children but only one level deep.

The messages table looks like this:

Messages
 - Id (PK, Auto-increment int)
 - UserId (FK, Users.Id)
 - FromUserId (FK, Users.Id)
 - ParentMessageId (FK to Messages.Id)
 - MessageText (varchar 200)

I’d like to show messages on a page with each ‘parent’ message followed by a collapsed view of the children messages.

Can I use the GROUP BY clause or similar construct to retrieve parent messages and children messages all in one query? Right now I am retrieving parent messages only, then looping through them and performing another query for each to get all related children messages.

I’d like to get messages like this:

Parent1
 Child1
 Child2
 Child3
Parent2
 Child1
Parent3
 Child1
 Child2
  • 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-01T20:09:51+00:00Added an answer on June 1, 2026 at 8:09 pm

    You can use a temporary ID to order the messages. If the message is a Parent then the temporary ID will be equal to the ID, else the temporary ID will be equal to the ParentMessageID. Then you just need to order by the temporary ID

    SELECT Messages.*, 
    CASE WHEN ParentMessageId IS NULL THEN Id ELSE ParentMessageId END AS tempId 
    FROM Messages
    ORDER BY tempId
    

    Edit

    If you want the first 10 records you could get the Ids first and then run the query

    SELECT Messages.*, 
    CASE WHEN ParentMessageId IS NULL THEN Id ELSE ParentMessageId END AS tempId 
    FROM Messages
    WHERE Messages.tempId IN (SELECT Messages.Id 
                          FROM Messages
                          WHERE ParentMessageId IS NULL
                          LIMIT 10
                          ORDER BY Messages.Id )
    ORDER BY tempId
    

    This way you only get the messages and respective childs from the first 10 messages.

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

Sidebar

Related Questions

my site has three div elements, header, content and footer. i have a picture
Each user of my site has their own profile page, which contains several items
I have a simple messaging system on my site. I would like to implement
I have a non-Drupal site which authenticates with a simple MySQL user database. I
My site has its own login system and the user can choose to connect
I am still a newbie.So that being said, my site has a messaging system
Assume we have a popular site. We need to implement mail-like messaging between users.
My web site has an opt-in form for user's email addresses. The form will
Each article of my site has a unique ID number. When a user clicks
On a site that has a fair share of user-generated content such as forum

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.