如何更新来自另一个表的 TOP 1 的列 [英] How to update column coming from TOP 1 of another table

查看:27
本文介绍了如何更新来自另一个表的 TOP 1 的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子:

  • City 表 - 列 CityIDNamePeriod
  • Assets 表 - 列 AssetID, Name
  • City table - columns CityID, Name, Period
  • Assets table - columns AssetID, Name

我必须使用 Assets 表的 AssetID 更新 City 表的 PeriodCity.Name=Assets.Name 的前 1 个.Assets 表对不同的资产具有相同的名称.

I have to update the Period column of the City table with AssetID of the Assets table matching with the top 1 where City.Name=Assets.Name. The Assets table have identical names for different assets.

示例 Assets 表:

AssetID  Name
1        Asset1
2        Asset1
3        Asset2
4        Asset2

我该怎么做?我尝试了不同的查询,但我无法得到它.

How can I do this? I tried with different queries but I am not able to get it.

推荐答案

UPDATE City
SET Period = a.AssetID
FROM (SELECT TOP 1 AssetID, Name FROM Assets ORDER BY AssetID ASC) AS a
WHERE City.Name = a.Name;

这篇关于如何更新来自另一个表的 TOP 1 的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆