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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:11:53+00:00 2026-05-14T07:11:53+00:00

Hey guys I have a query that currently finds the latest comment for each

  • 0

Hey guys I have a query that currently finds the latest comment for each of a user’s topics and then orders topics by that comment’s timestamp. What I want to do is expand on this query’s use and print the latest comment for each topic. The problem with this query is that while it orders the topics correctly, it prints seemingly random comments for each topic. I am trying to implement a sub query but I am not quite sure how to approach it. I was thinking that I just had to somehow use this query to get the comments. If anyone has any ideas I would really appreciate it.

Here is what I think I need to add

SELECT * FROM comments where topic_id='$topic_id' ORDER BY timestamp DESC LIMIT 1

Here is the query I need to modify

SELECT topic.topic_title, topic.content_type, topic.subject_id, topic.creator, topic.description, topic.topic_id,comments.message,comments.user
      FROM comments
      JOIN topic ON topic.topic_id = comments.topic_id
      WHERE topic.creator = '$user' AND comments.timestamp > $week
      GROUP BY topic_id ORDER BY MAX(comments.timestamp) DESC
  • 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-14T07:11:53+00:00Added an answer on May 14, 2026 at 7:11 am

    This is an example of the greatest-n-per-group problem, which comes up on Stack Overflow frequently. Follow the tag for more examples.

    SELECT t.topic_title, t.content_type, t.subject_id, t.creator, t.description, 
      t.topic_id, c1.message, c1.user
    FROM topic t
    JOIN comments c1 ON (t.topic_id = c1.topic_id)
    LEFT OUTER JOIN comments c2
      ON (t.topic_id = c2.topic_id AND c1.timestamp < c2.timestamp)
    WHERE t.creator = ? AND c1.timestamp > ?
      AND c2.topic_id IS NULL
    ORDER BY c1.timestamp DESC;
    

    PS: Use query parameters (?) for dynamic values, to reduce the risk of SQL injection.

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

Sidebar

Related Questions

Hey guys another rails issue, Currently have a collection that is line items for
Hey guys I have a query that selects data and organizes but not in
Hey guys I have an admin page that checks if you are admin before
Hey guys I have one more question lol. I am using a script that
Hey guys, I have developed a small site that i would like to embed
Hey guys, I have a program that uses ajax to send a post to
Hey guys, I have an application that I want to display some data from
Hey guys I have an ajax jquery function that receives data from a php
Hey guys, I have a program that allows me to run queries against a
Hey guys, I have the following HTML structure that I am trying to pull

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.