user1232939
user1232939

Reputation: 3

Sort Grouped By Results MySQL

I have a table which essentially an activity log for each user, I want to display how long it’s been since the last activity from the user.

Query:

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

Result

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

Problem: My Result is showing the oldest Time Stamp, and I want the most recent. Is there some way to ORDER within the GROUP BY?

Upvotes: 0

Views: 56

Answers (2)

user610217
user610217

Reputation:

You just need a 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

Upvotes: 2

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

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

Upvotes: 3

Related Questions