数据库表选取记录,谢谢

gdutcc 2005-09-21 05:06:25
有这样的数据库表:
id catalogid showid version
1 1 1 1
2 1 1 2
3 1 2 1
4 2 1 1
5 2 1 2
6 3 1 1

要选出同一个目录(catalogid)中同一个显示ID(showid)的版本号(version)最大的记录,在例子中结果是
id catalogid showid version
2 1 1 2
3 1 2 1
5 2 1 2
6 3 1 1

返回id,也可以。使用存储过程?
...全文
232 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdutcc 2005-09-21
  • 打赏
  • 举报
回复
谢谢,要有id>a.id。
not exists?
select 1 from 中的1 ?

初学者!
zjcxc 2005-09-21
  • 打赏
  • 举报
回复
select * from tb a
where not exists(
select 1 from tb
where catalogid=a.catalogid and showid=a.showid
and( version>a.version or version=a.version and id>a.id))
churchatp1 2005-09-21
  • 打赏
  • 举报
回复
select * from table1 t
where not exists(select 1
from table1
where catalogid=t.catalogid
and showid=t.showid
and version>t.version)
或者
select id=(select top 1 id from table1 b where b.version=a.version ),catalogid ,showid , max(version) from table1 a group by catalogid , showid
wgsasd311 2005-09-21
  • 打赏
  • 举报
回复
select * from tb a
where not exists( select 1 from tb where catalogid=a.catalogid and showid=a.showid
and version>a.version)
vivianfdlpw 2005-09-21
  • 打赏
  • 举报
回复
select * from 表 t
where not exists(select 1
from 表
where catalogid=t.catalogid
and showid=t.showid
and version>t.version)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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