从表 b 更新表 a (条件)

2022-01-17 00:00:00 sql-update sql tsql sql-server

晚上好,

其实是晚上.晚上11点左右.我的大脑正在关闭,我需要一些帮助才能完成并回家:)

Actually, it's night. About 11pm. My brain is shutting down and I need a bit of help so I can finish and go home :)

我有两个表 - 表 a 和表 b.当其他两个字段匹配时,我需要使用表 b 中字段的值更新表 a 中的字段.表格没有每条记录的唯一 ID :(

I have two tables - table a and table b. I need to update a field in table a with the value from a field in table b when two other fields match. The tables don't have a unique id for each record :(

基本上,我想这样做:

update a
set importantField = 
(select b.importantfield
from b
where a.matchfield = b.matchfield
and a.matchfield2 = b.matchfield2
)
where a.matchfield = b.matchfield
and a.matchfield2 = b.matchfield2

或者至少......我认为这就是我想做的......

Or at least... I think that's what I want to do...

有人可以帮帮我吗?

推荐答案

您可以通过加入更新来做到这一点:

You can do this via a join in the update:

Update a
Set a.importantField = b.importantField
From a Join b 
  On a.matchfield = b.matchfield
  And a.matchfield2 = b.matchfield2

相关文章