Select row with largest timestamp in each category

Viewed 9972

I have a MySQL database (blogs) that has a id, title, timestamp, and category_id column names. How can I select one row from each category that has the newest timestamp? If I have 3 rows being in category 1, 2, 1, then I will get two rows back with the highest timestamp of the categories in "1" as the first and the row with category "2" as the second.

I tried:

SELECT title, timestamp, category_id
FROM blogs 
WHERE timestamp IN (
  SELECT MAX(timestamp) 
  FROM blogs 
  GROUP BY category_id
)

BUT since timestamp is not unique, it could, say, pull in an extra row with category_id = 1 that has the same timestamp as the row with category_id = 2 that was selected in the inner select statement.

2 Answers
Related