求sql查询语句

qxking 2011-06-17 09:14:58
有两张表TSoftware(项目表)、TUpdateInfo(版本表)。
表TSoftware有五个字段:
software_id(int) 软件ID
software_name(varchar(50)) 软件名
founder(varchar(50)) 发布方
time(datetime) 时间
remarks(varchar(50)) 备注
其中software_id是主键。

表TUpdateInfo有6个字段
version_id(int) 版本ID
software_id(int) 项目ID
version(varchar(50)) 版本号
path(varchar(50)) 路径
time(varchar(50)) 时间
founder(varchar(50)) 发布方
其中software_id, version是主键
software_id是外键,这个外键的主键列是TSoftware中的software_id。

我现在想查询项目的最新版本即得到 TSoftware 中的 TSoftware 和 TUpdateInfo中 最新的version。
请问这个语句该怎么写?在线急等答案!谢谢!
...全文
46 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 元老 2011-06-17
  • 打赏
  • 举报
回复

select *
from TSoftware a
left join
(
select *
from TUpdateInfo t
where not exists (select 1 from TUpdateInfo where version = t.version and software_id = t.software_id and time > t.time)
)b
on a.software_id = b.software_id
fredrichku 2011-06-17
  • 打赏
  • 举报
回复
用apply,不过要2005+

select a.*,b.*
from TSoftware a
outer apply (
select top 1 *
from TUpdateInfo c
where a.software_id=b.software_id
order by time desc
) as b
Rotel-刘志东 2011-06-17
  • 打赏
  • 举报
回复
表的关联查询
FlySQL 2011-06-17
  • 打赏
  • 举报
回复
select a.*,b.*
from TSoftware a
join TUpdateInfo b on a.software_id=b.software_id
where
not exists(select 1 from TSoftware where software_id=a.software_id and time>a.time)
and
not exists(select 1 from TUpdateInfo where software_id=b.software_id and time>b.time)

34,590

社区成员

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

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