select * from t1 inner join (select 箱号,max(时间) as 时间 from t1 group by 箱号) t2 on t1.箱号=t2.箱号 and t1.时间=t2.时间
select * from t1 inner join (select 箱号,max(时间) as 时间 from t1 group by 箱号) t2 on t1.箱号=t2.箱号 and t1.时间=t2.时间
create table a( 箱号 varchar(10), 更新时间 datetime )insert into a values('001','2005-05-06') insert into a values('001','2005-05-05') insert into a values('001','2005-05-07') insert into a values('002','2005-05-05') insert into a values('002','2005-05-08') insert into a values('002','2005-05-09') insert into a values('003','2005-05-02') insert into a values('003','2005-05-05')select t1.* from a t1 where exists( select 1 from a t2 where t1.箱号=t2.箱号 having max(t2.更新时间)=t1.更新时间 )
select a.箱号,b.时间 from t1 a, (select 箱号,max(时间) as 时间 from t1 group by 箱号) b where a.箱号=b.箱号 and a.时间=b.时间
select a.* from 表 a where not exists(select 1 from 表 where 箱号=a.箱号 and 时间>a.时间)
from t1 inner join (select 箱号,max(时间) as 时间 from t1 group by 箱号) t2
on t1.箱号=t2.箱号 and t1.时间=t2.时间
from t1 inner join (select 箱号,max(时间) as 时间 from t1 group by 箱号) t2
on t1.箱号=t2.箱号 and t1.时间=t2.时间
箱号 varchar(10),
更新时间 datetime
)insert into a values('001','2005-05-06')
insert into a values('001','2005-05-05')
insert into a values('001','2005-05-07')
insert into a values('002','2005-05-05')
insert into a values('002','2005-05-08')
insert into a values('002','2005-05-09')
insert into a values('003','2005-05-02')
insert into a values('003','2005-05-05')select t1.* from a t1
where exists(
select 1 from a t2
where t1.箱号=t2.箱号
having max(t2.更新时间)=t1.更新时间
)
(select 箱号,max(时间) as 时间 from t1 group by 箱号) b
where a.箱号=b.箱号 and a.时间=b.时间
a.*
from
表 a
where
not exists(select 1 from 表 where 箱号=a.箱号 and 时间>a.时间)