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

  • Home
  • SEARCH
  • 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 8337835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:24:36+00:00 2026-06-09T04:24:36+00:00

I have a MySQL database (blogs) that has a id, title, timestamp, and category_id

  • 0

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.

  • 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-06-09T04:24:37+00:00Added an answer on June 9, 2026 at 4:24 am

    For MySQL, this query will return the result set you specified:

    SELECT b.title
         , MAX(b.timestamp)
         , b.category_id
      FROM blogs b
     GROUP BY b.category_id
    

    If there happen to be more than one “title” in a category that has the same latest “timestamp”, only one row for that category will be returned, so you will get just one “title” returned for each category.


    Note: other DBMS system will throw an exception (error) with a query like the one above, because of the handling of non-aggregates in the SELECT list that don’t appear in the GROUP BY.

    Your query was very close. You’ve already got that inner query returning the “latest” timestamp for each category. The next step is to return the category_id along with that latest timestamp.

    SELECT category_id, MAX(timestamp) AS timestamp
      FROM blogs
     GROUP BY category_id
    

    The next step is to join that back to blogs, to get the associated “title”(s)

    SELECT b.title
         , b.timestamp
         , b.category_id
      FROM (SELECT category_id, MAX(timestamp) AS timestamp
              FROM blogs 
             GROUP BY category_id
           ) l
      JOIN blogs b
        ON b.category_id = l.category_id AND b.timestamp = l.timestamp 
    

    NOTE: if there is more than one “latest” row for a category (the timestamp values match), this query will return both of them.

    If that’s a concern, the query can be modified (or written in a different way) to prevent any possibility of two rows for a category.


    Simply adding a GROUP BY clause to that query will work (in MySQL only, not other DBMSs)

    SELECT b.title
         , b.timestamp
         , b.category_id
      FROM (SELECT category_id, MAX(timestamp) AS timestamp
              FROM blogs 
             GROUP BY category_id
           ) l
      JOIN blogs b
        ON b.category_id = l.category_id AND b.timestamp = l.timestamp
     GROUP BY b.timestamp, b.category_id
    

    (For other DBMSs, you could modify the SELECT list, replace b.title with MAX(b.title) AS title. That will work when you are returning a single column from the row.

    If you want the rows returned in a particular order, add an ORDER BY clause.


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

Sidebar

Related Questions

I have mysql database and I want a software which can draw the database
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a mySQL database with a FirstName and LastName column and presently i
I recently added a new column to a MySQL database (featured), then added that
I have the above models in my MySQL database: Blogs (id: integer, name: varchar)
I have mysql database like this id | code 1 | bok-1 2 |
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
Here my code, I need to insert into mysql database I have mysql,php,android 1)
I have a MySQL database where the table was set up to store the
I have a MySQL database with 3 columns: id | articletitle | articleorganization And

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.