id   类型   状态    时间
111   1      0      2009-01-01 12:12:12
111   1      1      2009-01-02 12:12:12
111   1      0      2009-01-03 12:12:12
112   2      0      2009-01-01 12:12:12
112   2      1      2009-01-02 12:12:12
113   1      0      2009-01-03 12:12:12
114   1      1      2009-01-04 12:12:12
115   3      0      2009-01-05 12:12:12
115   3      1      2009-01-06 12:12:12
以id分组,类型相同,时间最新的一条数据
id为111的为例,只要111类型1时间为3号的那条数据

解决方案 »

  1.   

    select t.* from t, (select id,max(时间) s from t group by id ) tt where t.id=tt.id and t.时间 = tt.s
      

  2.   

    select * from t t1 where not exists(
    select 1 from t where id=t1.id and type=t1.type and ttime>t1.ttime);
      

  3.   

    select a.* from tb_name a where not exists (select 1 from tb_name b where b.id=a.id and b.类型=a.类型 and b.时间>a.时间)
      

  4.   

    select *
    from table1 a
    where not exists (select 1 from table1 where id=a.id and 类型=a.类型 and 时间>a.时间)