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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:01:26+00:00 2026-05-31T07:01:26+00:00

Hi I’m having problems with an SQL query. If I wanted output the table

  • 0

Hi I’m having problems with an SQL query. If I wanted output the table so that it displayed the products from latest comment made to earliest comment made…keeping in mind that if the output is product id 78, product id 79, product id 81, product id 78, product id 81, i want the output to be product id 78, product id 79, product id 81. Basically i’m trying to create a page showing the latest comment activity on a product.

SELECT comments.products_products_id, products.name, products.minilocation, products.users_user_id, 
comments.comment, comment_id, comments.time_stamp, users.username, users.miniavatar  FROM products
INNER JOIN comments ON comments.products_products_id = products.products_id INNER JOIN users ON users.user_id = comments.products_users_user_id
ORDER BY comments.time_stamp 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-31T07:01:27+00:00Added an answer on May 31, 2026 at 7:01 am

    Depends if you just want the most recent comment per product_id, or if you want all comments, but just sorted by product_id where the product with the latest comment is first.


    If you want all comments, grouped by product_id, where the product_ids are sorted in order of latest comment (i.e. see how the latest time stamp order suggest the products are in order (3,1,2):

    product_id    comment_timestamp  
       3              2012-03-13
       3              2012-03-09
       3              2012-03-01
       1              2012-03-12
       1              2012-01-01
       2              2012-03-11
    

    Then here’s how you can do it — find out the latest timestamp per product, JOIN that onto your main query, and order by it:

    SELECT comments.products_products_id, products.name, 
           products.minilocation, products.users_user_id, 
           comments.comment, comment_id, comments.time_stamp, 
           users.username, users.miniavatar  
    FROM products
    -- NEW JOIN:
    INNER JOIN (SELECT product_id, MAX(time_stamp) as latest 
                FROM comments 
                GROUP BY product_id) latest 
          ON latest.product_id = products.product_id
    -- as before.
    INNER JOIN comments ON comments.products_products_id = products.products_id 
    INNER JOIN users ON users.user_id = comments.products_users_user_id
    -- NEW SORT:
    ORDER BY latest.latest DESC, comments.time_stamp DESC
    

    The new table joined to is the maximum (i.e. most recent) time stamp per product id.
    This will be the same within a single product id.

    We sort by this first (to sort by the latest comment timestamp), and then sort by individual comment timestamp within each product id.


    If you want the most recent comment per product_id, i.e.

    product_id    comment_timestamp  
       3              2012-03-13
       1              2012-03-12
       2              2012-03-11
    

    Then use:

    SELECT comments.products_products_id, products.name, 
           products.minilocation, products.users_user_id, 
           comments.comment, comment_id, comments.time_stamp, 
           users.username, users.miniavatar  
    FROM comments
    -- NEW SELF-JOIN
    LEFT JOIN comments c2 ON comments.products_products_id = c2.products_products_id
                         AND comments.time_stamp < c2.time_stamp
    INNER JOIN products ON comments.products_products_id = products.products_id 
    INNER JOIN users ON users.user_id = comments.products_users_user_id
    -- new condition
    WHERE c2.time_stamp IS NULL
    ORDER BY comments.time_stamp DESC
    

    This sort of query is called “greatest n per group”, and you basically join your COMMENTS table to itself within each product_id. You also join it such that one table has time_stamps less than the other’s. The WHERE condition selects rows such that there is no greater time stamp for that product_id, i.e. the latest comment.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

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.