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

我有一个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
);

注意:评论后更正

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...