sql请教,在线等!!!急,急,急!!!!!!!

「已注销」 2008-10-10 11:01:50
表描述:table_yyy ,两个关键字段 devid , time
devid time desc
1 20080910 xx
1 20081013 xx
1 20081021 xx
2 20080913 xx
3 20080910 xx
3 20081013 xx

需要查询出最接近当前时间的,各个设备的的记录!
查询结果
devid time desc
1 20081021 xx
2 20080913 xx
3 20081013 xx

sql语句怎么写,谢谢!
...全文
65 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
passerbyang 2008-10-10
  • 打赏
  • 举报
回复
第一种
select * from table_yyy a where not exists(select 1 from table_yyy where device=a.device and time>a.time) order by desc
第二种
select * from table_yyy a where time=(select max(time) from table_yyy where device=a.device) order by desc
试试行不行吧
hsie168518 2008-10-10
  • 打赏
  • 举报
回复
select * from table_yyy a,
(select devid, max(time) as time from table_yyy group by devid)b
where a.devid=b.devid and a.time=b.time
昵称被占用了 2008-10-10
  • 打赏
  • 举报
回复
select * from table_yyy y
where not exists (
select 1
from table_yyy
where devid=y.devid
and time>y.time
)
hsie168518 2008-10-10
  • 打赏
  • 举报
回复
select devid max(time) as time desc from table_yyy group by devid
水族杰纶 2008-10-10
  • 打赏
  • 举报
回复
select * from table_yyy yy where not exists(select 1 from table_yyy where devid=yy.devid and time >yy.time )
dlpseeyou 2008-10-10
  • 打赏
  • 举报
回复
select * from table_yyy order by time desc
dlpseeyou 2008-10-10
  • 打赏
  • 举报
回复
select * from table_yyy order by time desc

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧