按问题统计和分组

时间:2013-07-10 11:45:41

标签: mysql count group-by

我有那张桌子:

论坛:

 _____________________________________________________________
|match_static_id| comment   | timpstamp            | user_id  |
|_______________|___________|______________________|__________|
|  1            |  Hi       | 2013-07-10 12:15:03  |     2    |
|  1            |  Hello    | 2013-07-09 12:14:44  |     1    | 
|_______________|___________|______________________|__________|

工作查询是:

select forum.match_static_id, 
       count(forum.match_static_id) 'comment_no' 
Group By forum.match_static_id 

但是,如果我想拥有:

select forum.match_static_id, 
   count(forum.match_static_id) 'comment_no',
   forum.timestamp
Group By forum.match_static_id 

它将提供与上一个查询相同的结果,但每个记录的值为timestamp

我希望这个值能够成为最新的时间戳吗?

2 个答案:

答案 0 :(得分:0)

只需使用max()函数。

select forum.match_static_id, 
   count(forum.match_static_id) 'comment_no',
   max(forum.timestamp)
Group By forum.match_static_id

以下是可用aggregate functions的列表和说明。

答案 1 :(得分:0)

这个怎么样:

select forum.match_static_id, 
count(forum.match_static_id) 'comment_no',
max(forum.timestamp)
Group By forum.match_static_id