Jump to content

Selecting most recent by Timestamp


cg5572

Recommended Posts

Ok I have a really rookie question which for some reason I can't get my head around:

 

I have a table which stores user comments, with the following fields:

 

- comment_id

- post_id

- user_id

- category_id

- timestamp

 

I'm currently using the following query to return all comments by a user in a given category:

 

SELECT * FROM comments_table WHERE user_id='$user' AND category='$category'

 

What I want to be able to do is retrieve the most recent comment for each post, i.e. if the above query returns multiple comments for the same post_id, show only the most recent one (highest timestamp).

 

Is there a way to build this into the query, or if not what is the best way of extracting them in the PHP afterwards?

Link to comment
https://forums.phpfreaks.com/topic/210964-selecting-most-recent-by-timestamp/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.