加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 如何仅选择表中的最新条目?

发布时间:2020-12-12 16:23:39 所属栏目:MsSql教程 来源:网络整理
导读:我有一个3表SQLServer数据库. ProjectProjectIDProjectNameThingThingIDThingNameProjectThingLinkProjectIDThingIDCreatedDate 当Thing归于Project时,条目将放入ProjectThingLink表中. 事情可以在项目之间移动. CreatedDate用于了解上次移动的项目. 我正在尝
我有一个3表SQLServer数据库.
Project
ProjectID
ProjectName

Thing
ThingID
ThingName

ProjectThingLink
ProjectID
ThingID
CreatedDate

当Thing归于Project时,条目将放入ProjectThingLink表中.
事情可以在项目之间移动. CreatedDate用于了解上次移动的项目.

我正在尝试创建一个目前与之相关联的所有项目的列表,但我的大脑失败了.

有这么简单的方法吗?

解决方法

select p.projectName,t.ThingName
from projects p
join projectThingLink l on l.projectId = p.projectId
join thing t on t.thingId = l.thingId
where l.createdDate =
( select max(l2.createdDate)
  from projectThingLink l2
  where l2.thingId = l.thingId
);

注意:评论后更正

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读