Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

mysql - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...