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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:54:51+00:00 2026-05-27T06:54:51+00:00

I got a table with threads and one with posts. I want to list

  • 0

I got a table with threads and one with posts. I want to list these ordered by the number of post associated to them.

Example structure of my tables

Posts table:

id   creator    replyTo    text    timestamp
1    1          1          Bla     2011-11-11 11:11
2    2          2          Alb     2011-11-11 11:11
3    3          3          Lba     2011-11-11 11:11
4    4          1          Lab     2011-11-11 11:11
5    5          2          Bal     2011-11-11 11:11
6    2          2          Abl     2011-11-11 11:11

Threads table:

id  creator     name       content    timestamp
1   1           BlaBla     BlaBla     2011-11-11 11:11
2   3           AlbAlb     AlbAlb     2011-11-11 11:11
3   2           LbaLba     LbaLab     2011-11-11 11:11

Example output:

id  creator     name       count      timestamp
2   3           AlbAlb     3          2011-11-11 11:11
1   1           BlaBla     2          2011-11-11 11:11
3   2           LbaLba     1          2011-11-11 11:11
  • 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-27T06:54:52+00:00Added an answer on May 27, 2026 at 6:54 am

    Try this:

    SELECT t.id, t.creator, t.name, count(*) AS ct, t.timestamp
    FROM   threads t
    JOIN   posts p ON p.replyTo = t.id
    GROUP  BY 1
    ORDER  BY count(*) DESC, 1,2;
    

    Produces exactly the requested results.
    See the working demo here.

    GROUP  BY 1
    

    is short for (because it is enough to group by the primary key in mysql):

    GROUP  BY 1,2,3,5
    

    which is short for (because these are positional parameters):

    GROUP  BY t.id, t.creator, t.name, t.timestamp
    

    Answer to additional question in comment

    Add a WHERE clause like this:

    SELECT t.id, t.creator, t.name, count(*) AS ct, t.timestamp
    FROM   threads t
    JOIN   posts p ON p.replyTo = t.id
    WHERE  t.timestamp BETWEEN (CURRENT_TIMESTAMP - INTERVAL 24 HOUR)
                           AND CURRENT_TIMESTAMP 
    GROUP  BY 1
    ORDER  BY count(*) DESC, 1,2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got two tables: threads: id, title posts: id, thread_id, body I want
I've got a table of URLs and I don't want any duplicate URLs. How
I've got a table structure I'm not really certain of how to create the
I insert in one table with many concurrent threads, in this Table is a
I've got a java.util.List bind to a JTable, if I want to refresh the
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
I got a table with a custom TableCellEditor (extending DefaultCellEditor) with a JFormattedTextField as
I got a table where in got allocated space of 3gig but have no
If I've got a table containing Field1 and Field2 can I generate a new
I've got a table that is supposed to track days and costs for shipping

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.