排序按结果分组MySQL

时间:2012-05-02 12:46:47

标签: mysql sql

我有一个表,基本上是每个用户的活动日志,我想显示自用户上一次活动以来的时间。

查询:

SELECT message_log.log_phone_number, message_log.log_updated
FROM message_log
WHERE message_log.log_mode = “inbound”
GROUP BY  message_log.log_phone_number

结果

415407XXXX  2012-03-07 13:34:14      
434242XXXX  2012-03-07 16:00:42          
434465XXXX  2012-03-07 14:49:15          
434989XXXX  2012-03-07 15:30:22          
757615XXXX  2012-03-07 15:30:54          
804651XXXX  2012-03-07 14:13:04          
920917XXXX  2012-03-07 15:11:28

问题:我的结果显示最早的时间戳,我想要最新的时间戳。 ORDER中有GROUP BY的某种方法吗?

2 个答案:

答案 0 :(得分:3)

SELECT message_log.log_phone_number, max(message_log.log_updated) 
FROM message_log 
WHERE message_log.log_mode = “inbound” 
GROUP BY message_log.log_phone_number

答案 1 :(得分:2)

你只需要一个MAX()。

SELECT message_log.log_phone_number, 
MAX(message_log.log_updated) as log_updated
FROM message_log
WHERE message_log.log_mode = “inbound”
GROUP BY  message_log.log_phone_number