从表 b 更新表 a 其中(条件)

发布于 2024-08-19 16:54:36 字数 442 浏览 5 评论 0原文

晚上了,

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

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

基本上,我想这样做:

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

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

有人可以帮我吗?

Evening all,

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 :)

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 :(

Basically, I want to do this:

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...

Can someone help me out, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-08-26 16:54:36

您可以通过加入更新来执行此操作:

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

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
云柯 2024-08-26 16:54:36

使用:

UPDATE TABLE_A
   SET importantField = (SELECT b.importantfield
                           FROM TABLE_B b
                          WHERE b.matchfield = matchfield
                            AND b.matchfield2 = matchfield2) 

SQL Server 不支持正在更新的表上的表别名,但上面是一个相关查询 - 那些没有附加表别名 b 的字段将提供来自 TABLE_A 的值> 因为它没有别名。

除此之外的唯一问题是,与 TABLE_A 匹配的记录是否存在多个 b.importantfield 值。使用:

UPDATE TABLE_A
   SET importantField = (SELECT TOP 1 
                                b.importantfield
                           FROM TABLE_B b
                          WHERE b.matchfield = matchfield
                            AND b.matchfield2 = matchfield2) 

..但您也应该使用 ORDER BY ,否则您将获得任何随机的 b.importantfield 值。

Use:

UPDATE TABLE_A
   SET importantField = (SELECT b.importantfield
                           FROM TABLE_B b
                          WHERE b.matchfield = matchfield
                            AND b.matchfield2 = matchfield2) 

SQL Server doesn't support table aliases on the table being updated, but the above is a correlated query - those fields without the table alias b attached will serve values from TABLE_A because it doesn't have an alias.

The only issue beyond that is if there are multiple b.importantfield values for records with the matching records to TABLE_A. Use:

UPDATE TABLE_A
   SET importantField = (SELECT TOP 1 
                                b.importantfield
                           FROM TABLE_B b
                          WHERE b.matchfield = matchfield
                            AND b.matchfield2 = matchfield2) 

..but you should use an ORDER BY as well or you'll get any random b.importantfield value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文