现有一张表(有ID和提),取出最新操作的数据,sql怎么写?

现有一张表(有ID和操作时间time),因为编辑时间的不同,相同ID的数据会有多条记录,我怎么才能把最新操作的那条数据取出来,sql语句该怎么写?

select id,max(time) from table
group by id;——这个语句是把表中所有记录每一个id的最大操作时间取出来
select id,time from table
order by time desc;——这个语句是把表中的记录按时间倒序排序,第一条记录即为最近一次操作的那条数据。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-09-21
select * from table as a where exists
(select 1 from table where a.id=id group by id
having max(time)=a.time)本回答被网友采纳
第2个回答  2012-09-21
select top 1 from table order by time desc

DESC是排序,把你最后的时间,也是最新的一条,然后top 1是取这条数据。
第3个回答  2012-09-21
这个肯定行
select *
from tab t
where not exists (select 1 from tab where ID = t.ID and time > t.time)

如果要最早的纪录,只要把>换为<就可以了
第4个回答  2012-09-21
你按时间排序,然后拿出第一条。
相似回答