日期:2014-05-17 浏览次数:20445 次
create table tb_order(orderid int,userid int,createtime datetime)
insert tb_order
select 1,1,'2012-09-12 12:00:01'
union select 2,1,'2012-09-12 13:00:01'
union select 3,1,'2012-09-13 13:00:01'
union select 4,2,'2012-09-12 13:00:01'
union select 5,2,'2012-09-13 13:00:01'
union select 6,3,'2012-09-13 13:00:01'
union select 7,3,'2012-09-14 13:00:01'
select a.* from tb_order a join
(select userid,MAX(createtime)createtime from tb_order
group by userid) b on a.userid =b.userid and a.createtime =b.createtime
/*
orderid userid createtime
7 3 2012-09-14 13:00:01.000
5 2 2012-09-13 13:00:01.000
3 1 2012-09-13 13:00:01.000
*/