Mysql 来自其他列的值的最大函数问题

Mysql 来自其他列的值的最大函数问题,mysql,groupwise-maximum,Mysql,Groupwise Maximum,我有以下MySQL查询: Select match_static_id, comments as last_comment, max(timestamp) from comments as c group by match_static_id; 我有一张关于比赛的评论表,我想知道每场比赛的最新评论。 因此,我使用max(timestamp)和group by(match_static_id)来实现这一点,但我的问题是,我得到了按match_static_id分组的max timestamp,

我有以下MySQL查询:

Select match_static_id, comments as last_comment, max(timestamp)
from comments as c 
group by match_static_id;
我有一张关于比赛的评论表,我想知道每场比赛的最新评论。 因此,我使用max(timestamp)和group by(match_static_id)来实现这一点,但我的问题是,我得到了按match_static_id分组的max timestamp,但我得到了其他注释(不是max timestamp的注释)
我的查询顺序是否错误?

我不是mysql专家,但我能感觉到这个问题。这可能是因为group by中的注释不是group by的一部分,它将返回与match_static_id匹配的所有行。我建议重写如下内容:

select match_static_id, (select Comment from comments c where c.timestamp =max(a.timestamp)) as last_comment, max(timestamp) from comments group by match_staic_id

这将解决:

SELECT match_static_id, comments AS last_comment, login_time2
  FROM  comments c
WHERE timestamp=
    ( SELECT MAX(timestamp) AS login_time2
      FROM comments WHERE match_static_id=c.match_static_id)
 GROUP BY match_static_id;

请提供一些输入输出模式。如果您能提供此查询的演示将非常有用,因为它提供了所有注释我只想获得具有最大时间戳的匹配的注释有
我想获得每个匹配的最新注释有
每个
-naswer中有简单的更新。好的,我的朋友让我在那里澄清一下这是一种误解。我希望每个匹配的最后一条注释都是正确的,但是在您的第一个查询中,我得到了一个匹配的所有注释,它们都具有相同的时间戳(最大时间戳)。我只想要一个评论的每一场比赛,这是一个与最大的时间戳。我希望我现在能说清楚哦,我明白了。现在看。
SELECT match_static_id, comments AS last_comment, login_time2
  FROM  comments c
WHERE timestamp=
    ( SELECT MAX(timestamp) AS login_time2
      FROM comments WHERE match_static_id=c.match_static_id)
 GROUP BY match_static_id;