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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:31:09+00:00 2026-05-25T22:31:09+00:00

I have two tables: a Topics tables and a joined Comments tables, both with

  • 0

I have two tables: a Topics tables and a joined Comments tables, both with timestamp columns. I would like to order the topics list so that topics with the most recent comment are at the top. However, if a topic has no comments, I want it to be sorted by the date it was created.
In other words I’m trying to order it so that the topics are sorted by the date they were created only if they have no comments – basically like any forum. For example if topic A (empty) was created after topic B (not empty) but topic B has a reply that was most recent the order should be B, A. I don’t want all the empty topics at the top if they’re old or at the bottom if they’re new.

I tried the IF ISNULL statement but it applies to the entire column and not each individual row so I end up with the empty threads either stuck at the top or bottom of the feed.

I’m guessing I’d have to construct a virtual column with only the most recent comment from each topic…?

Here’s the full statement:

SELECT
  $showbody,
  Topics.Title,
  Topics.id AS tID,
  Topics.Timestamp,
  Topics.MemberID,
  Users.id,
  Users.FirstName,
  Users.LastName,
  GROUP_CONCAT(DISTINCT Tags.Keywords SEPARATOR ', ') AS Tags,
  COUNT(Comments.id) AS NumberOfComments,
  (
    SELECT COUNT(Comments.id)
    FROM Comments
      LEFT JOIN Views ON Comments.TopicID = Views.TopicID
    WHERE Comments.Timestamp > Views.Visited
  ) AS NewComments
FROM Topics 
  LEFT JOIN Users ON Topics.MemberID = Users.ID 
  LEFT JOIN Comments ON Topics.id = Comments.TopicID 
  LEFT JOIN Tags ON Topics.id = Tags.TopicID 
WHERE Topics.id NOT IN (
  SELECT Tags.TopicID
  FROM Tags
  WHERE Keywords IN (
    SELECT Tag
    FROM Filters
    WHERE MemberID = '$_SESSION[SESS_MEMBER_ID]'
  )
  GROUP BY Tags.TopicID
) 
GROUP BY Topics.id 
ORDER BY Comments.Timestamp, Topics.Timestamp DESC LIMIT $plim

Any help would be greatly appreciated

  • 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-25T22:31:10+00:00Added an answer on May 25, 2026 at 10:31 pm

    Solution 1 (demo):

    -- Test initialization
    CREATE TABLE Question
    (
         QuestionID INT AUTO_INCREMENT PRIMARY KEY
        ,Content NVARCHAR(100) NOT NULL
        ,CreateDate DATETIME NOT NULL -- DEFAULT NOW()
    );
    
    CREATE TABLE QuestionComment
    (
         QuestionCommentID INT AUTO_INCREMENT PRIMARY KEY
        ,QuestionID INT NOT NULL REFERENCES Question(QuestionID)
        ,Content NVARCHAR(100) NOT NULL
        ,CreateDate DATETIME NOT NULL -- DEFAULT NOW()
    );
    
    INSERT  Question(Content, CreateDate)
    SELECT  'Question 1','2011-01-01 01:00:00'
    UNION ALL
    SELECT  'Question 2','2011-01-01 02:00:00'
    UNION ALL
    SELECT  'Question 3','2011-01-01 03:00:00';
    
    INSERT  QuestionComment(QuestionID, Content, CreateDate)
    SELECT  1,'Comment 1.1','2011-01-01 01:30:00'
    UNION ALL
    SELECT  2,'Comment 2.1','2011-01-01 02:30:00'
    UNION ALL
    SELECT  1,'Comment 1.2','2011-01-01 02:40:00'
    UNION ALL
    SELECT  2,'Comment 2.2','2011-01-01 02:30:00'
    UNION ALL
    SELECT  1,'Comment 1.3','2011-01-01 03:30:00';
    -- End of Test initialization
    
    -- Solution
    SELECT  *
    FROM    Question q
    LEFT JOIN
    (
        SELECT  qc.QuestionID, MAX(qc.CreateDate) LastCreateDate
        FROM    QuestionComment qc
        GROUP BY qc.QuestionID
    ) qc    ON q.QuestionID=qc.QuestionID
    ORDER BY IFNULL(qc.LastCreateDate, q.CreateDate) ASC;
    -- End of Solution
    
    -- By, by
    DROP TABLE QuestionComment;
    DROP TABLE Question;
    

    Results:

    2   Question 2  2011-01-01 02:00:00 2   2011-01-01 02:30:00
    3   Question 3  2011-01-01 03:00:00     
    1   Question 1  2011-01-01 01:00:00 1   2011-01-01 03:30:00
    

    Solution 2:

    SELECT  *
    FROM    Topics t
    LEFT JOIN
    (
        SELECT  c.TopicID, MAX(Timestamp) LastTimestamp
        FROM    Comments c
        GROUP BY c.TopicID 
    ) c ON t.id = c.TopicID 
    ORDER BY IFNULL(c.LastTimestamp, t.Timestamp) ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that are joined together. A has many B Normally you
I have two tables called categories and topics. categories has columns category_id and category_title.
I have two tables called 'events' and 'topics' each table can have many comments.
I have two tables named MEMBER - columns id(primary key), name, email & TOPICS
I have two tables, both with start time and end time fields. I need
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have two tables say, t1 and t2 that have t1.t1_id and t2.t2_id as
I have two tables, subscriptions and topics. Each subscription is related to a specific
For example I have 2 tables: topics and comments. topic has_many comments. comment belongs_to

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.