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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:33:32+00:00 2026-05-16T14:33:32+00:00

I have created a feature where users can start new topics (similar to forums).

  • 0

I have created a feature where users can start new topics (similar to forums).
At the moment on a page, the query for the topics are as follows:

$q = "SELECT ".TBL_COMMUNITYTHREADS.".title, ".TBL_COMMUNITYTHREADS.".id, 
    ".TBL_COMMUNITYTHREADS.".date, ".TBL_COMMUNITYTHREADS.".author, ".TBL_USERS.".username FROM ".TBL_COMMUNITYTHREADS." 
        INNER JOIN ".TBL_USERS." ON ".TBL_COMMUNITYTHREADS.".author = ".TBL_USERS.".id
        WHERE type = '$type'
        ORDER BY date DESC LIMIT $offset, $rowsperpage ";

The tables are constants and the offset and rowsperpage are variables passed in to limit how many posts are on a page.

At the moment though, all the topics are ordered by the date.
I want them to be ordered by the latest response.
Similarly to forums, when the reponse inside the topic is newest, that topic will go to the top.

The topics are stored in tbl_communitythreads and the replies in tbl_communityreplies.

How can I ordered them by the latest repsonse.

They are linked by the threadid in tbl_communityreplies. Also in that one is the date column.

Thankyou for reading, I just cant think of how to do this.

  • 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-16T14:33:32+00:00Added an answer on May 16, 2026 at 2:33 pm

    This:

    SELECT  c.title, c.id, c.date, c.author, u.username,
            (
            SELECT  MAX(reply_date)
            FROM    tbl_communityreplies cr
            WHERE   cr.thread = c.id
            ) AS last_reply
    FROM    TBL_COMMUNITYTHREADS c
    JOIN    TBL_USERS u
    ON      u.id = c.author
    ORDER BY
            last_reply DESC, c.id DESC
    LIMIT   $offset, $rowsperpage
    

    or this:

    SELECT  c.title, c.id, c.date, c.author, u.username
    FROM    (
            SELECT  cr.thread, cr.reply_date, cr.id
            FROM    tbl_communityreplies cr
            WHERE   (cr.last_reply, cr.id) = 
                    (
                    SELECT  last_reply, id
                    FROM    tbl_communityreplies cri
                    WHERE   cri.thread = cr.thread
                    ORDER BY
                            thread DESC, last_reply DESC, id DESC
                    )
            ORDER BY
                    last_reply DESC, id DESC
            LIMIT   $offset, $rowsperpage
            ) q
    JOIN    TBL_COMMUNITYTHREADS c
    ON      c.id = q.thread
    JOIN    TBL_USERS u
    ON      u.id = c.author
    ORDER BY
            q.reply_date DESC, q.id DESC
    

    The former query is more efficient for large values of $offset, or if you have few threads with lots of replies.

    Issue the following commands:

    CREATE INDEX ix_communitythreads_replydate_id ON TBL_COMMUNITYTHREADS (reply_date, id)
    CREATE INDEX ix_communitythreads_thread_replydate_id ON TBL_COMMUNITYTHREADS (thread, reply_date, id)
    

    for this to work fast.

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

Sidebar

Related Questions

I have created a web template in visual studio that users can create sub
I have created a feature, a publishing site, in Visual Studio to MOSS -
I have created a search feature for a site and in addition I made
I have successfully created a feature in sharepoint that modifies the existing edit dialog
I have created a custom list as a feature in sharepoint. i need to
I've created a method using the new WebAPI features in MVC4 and have it
I have a rails app that users can upload photos to. The photos get
Using EntityFramework, I have created a page with a number of DetailsView and FormView
My web app has a feature where users can create a list of up
I have created a great stand-alone web form in asp.net utilizing many jQuery features

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.