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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:12:39+00:00 2026-05-14T06:12:39+00:00

I have this query, and I am trying to get the latest comment for

  • 0

I have this query, and I am trying to get the latest comment for each topic and then sort those results in descending order (therefore one comment per topic). I have what I think should work, but my join always messes my results up. Somehow, it seems to have sorted the end results properly, but has not taken the latest comment from each topic instead it seems to have just taken a random comment. If anyone has any ideas, would really appreciate any advice

SELECT * FROM comments 
JOIN topic ON topic.topic_id=comments.topic_id 
WHERE topic.creator='admin' 
GROUP BY comments.topic_id 
ORDER BY comments.time DESC

table comments is structured like
id time user message topic_id

table topic is structured like
**topic_id subject_id topic_title creator timestamp description**

  • 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-14T06:12:39+00:00Added an answer on May 14, 2026 at 6:12 am

    You’ve got a couple of things going on here. First, the reason your current query is returning weird results is that you aren’t really using your GROUP BY clause in the way intended; it is intended to be used with aggregrated fields (like COUNT(), SUM(), etc). It is a convenient side-effect that on MySQL, the GROUP BY clause also returns the first record that would be in the group–which, in your case, should be the first inserted message for each topic (not a random one). So your query as it is written is essentially returning the oldest messsage per topic (on MySql only; note that other RDBMS’s will throw an error if you try to use a GROUP BY clause like that!)

    But you can actually abuse the GROUP BY clause to get what you want, and you are really close already. What you need to do is to do a sub-query to make a derived table first (with your messages ordered by DESC date), then query the derived table using the GROUP BY clause like this:

    select * from (
      SELECT
        topic.topic_title, comments.id, comments.topic_id, comments.message
      FROM comments
      JOIN topic ON topic.topic_id=comments.topic_id
      WHERE topic.creator='admin'
      order by comments.time desc) derived_table
    group by topic_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with this query trying to get a sum and a
I am trying to have information displayed for this query: SELECT o.sub_number,o.unique_id,o.period_from,o.period_to,o.total_amt, i.paid_amt,i.dated,i.payment,i.paid_by,i.entered_date,i.paid_for_unique, j.cheque_num,j.drawn_on,j.dated
I have below query I am trying to show message 'No SubSource for this
I'm trying to rewrite some query strings I have in my URL like this
I have this sql query where I'm trying to use CONTAINS to search the
I'm trying to get the direction property for the element. I have this code:
I want to get latest two posts. I'm trying to use following query, but
I currently have this query: select customers.emailaddress, MAX(orders.orderdate) as last order from orders join
I've been trying to get this complex MYSQL query to work exactly right over
I have this query statement and want to only get records that has a

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.