SQL 用中Max函数取最新数据为何总是出现多条相同id,不同时间的记录?

2025-12-06 13:25:07
推荐回答(5个)
回答1:

方法很多
1.按你的写法 这么改

select sid,sdata,stime from hisdata a
where stime in (select max(stime) from hisdata where a.sid=sid);

2.用 Not exists

select sid,sdata,stime from hisdata a
where not exists(select * from hisdata where a.sid=sid and a.stime

回答2:

你group by sid

就是按sid分组取每组里面stime最大的sid

如果只想一条数据

就不加group by

回答3:

select max(stime) from hisdata group by sid )
你加个distinct 试试

回答4:

select sid,sdata,stime from hisdata where stime in (select max(stime) from hisdata)

回答5:

stime 不是唯一