简体   繁体   English

如何获取表格中每个类别的最新日期?

[英]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:解决方案是像这样使用聚合 function MAX()

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

Answer added on behalf of OP代表 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:要选择表中每个类别的最新日期,并且如果日期包含在last_reply列中,那么您可以尝试以下查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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