更新查询 - 查找最年轻的日期并更新另一个值
Update query - Find the youngest date and update another value
我可以访问 table tblExample,其中包含以下字段:ID、tripID、日期、值。
现在我正在 SQL 中寻找一个更新查询,它实现了这一点:获取 tripID = 2 的记录。从所有记录中获取最年轻的日期并设置 value=14。
应该很容易,但不要让它工作。感谢您的帮助。
您可以在 where
子句中使用子查询来过滤到您想要的行:
update tblExample
set value = 14
where tripID = 2 and
dates = (select max(e2.dates)
from tblExample as e2
where e2.tripID = tblExample.tripID
);
或基于id
:
update tblExample
set value = 14
where tripID = 2 and
id = (select top (1) e2.id
from tblExample as e2
where e2.tripID = tblExample.tripID
order by e2.dates desc
);
我可以访问 table tblExample,其中包含以下字段:ID、tripID、日期、值。
现在我正在 SQL 中寻找一个更新查询,它实现了这一点:获取 tripID = 2 的记录。从所有记录中获取最年轻的日期并设置 value=14。
应该很容易,但不要让它工作。感谢您的帮助。
您可以在 where
子句中使用子查询来过滤到您想要的行:
update tblExample
set value = 14
where tripID = 2 and
dates = (select max(e2.dates)
from tblExample as e2
where e2.tripID = tblExample.tripID
);
或基于id
:
update tblExample
set value = 14
where tripID = 2 and
id = (select top (1) e2.id
from tblExample as e2
where e2.tripID = tblExample.tripID
order by e2.dates desc
);