简体   繁体   中英

how to get the most recent date per category in a table?

I am trying to get the most recent date for every category. This is my table:

tables names:

threads table:
threadID
title
category
author
date_posted
post
last_author
last_reply
total_posts
isLocked
isSticky
$result = mysqli_query($conn, "SELECT * FROM threads
                               GROUP BY category
                               ORDER BY last_reply");

This gives me the date of the first row appearing in the table with that category. I want to be able to get the most recent date for that category not the first seen row. Thanks.

The solution was to use the aggregation function MAX() like this:

SELECT Max(last_reply) as recent FROM threads
GROUP BY category
ORDER BY last_reply

Answer added on behalf of OP

To choose the most recent date per category in the table and if the date is contained in the column last_reply then you can try the following query for the same:

SELECT category, Max(last_reply) as most_recent_date FROM threads GROUP BY category;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM