View Single Post
  #1 (permalink)  
Old 03-12-10, 11:17
chiefman chiefman is offline
Registered User
 
Join Date: Jan 2009
Posts: 23
write a right sql statement to get this job done

Hi guys, may I have your attention and help on this. Thanks!

The table "message(id, userId, content, createdTime)". now I need to find out those records of the latest message (namely the max createdTime) for each user, the returned results should be ordered by createdTime.
e.g
data in the table:
1 'eric' 'hello' '2009-12-30'
2 'lisa' 'yes' '2010-01-21'
3 'eric' 'thanks' '2009-10-06'
4 'lisa' 'hey' '2010-09-12'

I got one: select msg. * from (select m.* from message as m order by m.createdTime) as msg group by msg.userId order by msg.createdTime DESC. but I think it's not good due to low performance.

the expected results:
4 'lisa' 'hey' '2010-09-12'
1 'eric' 'hello' '2009-12-30'

Last edited by chiefman; 03-12-10 at 11:21.
Reply With Quote