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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:55:02+00:00 2026-06-03T06:55:02+00:00

I have a table with the following key fields: commentid , comment , time

  • 0

I have a table with the following key fields: commentid, comment, time, parentid

The comments are of two types, regular and replies. Regular have parentid 0; replies have parentid equal to the commentid they reply to.

I thought I had this figured out with group by and orderby but my idea didn’t work and I’m now struggling as my knowledge of sql is rudimentary.

I want it to display by time DESC except that I would like those with the same parentid grouped together and within that parentid, that they also be be sorted by time. I am allowing only one level of reply.

As an example, I would like following order:

time3 commentid3 parentid0
time2 commentid2 parentid0
    parentid2 commentid4 time4 (this one is a reply)
    parentid2 commentid5 time5 (this one also reply)
time1 comment1 parentid0

I tried SELECT * from comments GROUP BY parentid ORDER BY TIME DESC but this did not work. If needed, I can add another column. Any suggestions would be appreciated! Thx.

  • 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-03T06:55:03+00:00Added an answer on June 3, 2026 at 6:55 am

    I’m making a few assumptions here. I’m assuming your commentid is an auto-incrementing id, so that would mean that the insert order would be from oldest to newest. This will not work if you are not using auto-incrementing ids or if you have some kind of partial-save functionality with these tables. So it’s kind of fragile.

    I’m also assuming parent_id is null if it is the parent.

    SELECT commentid, comment, time, parent_id, if(parent_id = 0, commentid, parent_id) thread 
    FROM comments  
    ORDER BY thread desc, time asc
    

    Anyway to add some more information.

    Group By is not what you want to use because it will group all rows by the grouping column into one row. Group By is usually used for aggregate calculations such as counting or summing the values in rows, etc.

    EDIT:

    I updated the query to sort by time asc, which will put the regular comment first and then the replies below the parent comment from oldest to newest.

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

Sidebar

Related Questions

I have a table (session_comments) with the following fields structure: student_id (foreign key to
I have a table with the following fields: id VARCHAR(32) PRIMARY KEY, parent VARCHAR(32),
following on from my other post... I have a table that uses two fields
Suppose I have a table called spitems with the following fields: spitemid (unique key)
I have a table similar to the following: id key str_val date_val num_val 1
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have the following tables: create table TableA ( Id int primary key identity,
I have the following defining a table: CREATE TABLE players(playerid INTEGER PRIMARY KEY AUTOINCREMENT
I have two database tables with the following structure: actions: action_id int(11) primary key
I have a database table with the following fields item_key, item_value, display_name, uihint I

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.