从表 b 更新表 a 其中(条件)
晚上了,
其实,已经是晚上了。晚上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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过加入更新来执行此操作:
You can do this via a join in the update:
使用:
SQL Server 不支持正在更新的表上的表别名,但上面是一个相关查询 - 那些没有附加表别名
b
的字段将提供来自TABLE_A
的值> 因为它没有别名。除此之外的唯一问题是,与 TABLE_A 匹配的记录是否存在多个
b.importantfield
值。使用:..但您也应该使用
ORDER BY
,否则您将获得任何随机的b.importantfield
值。Use:
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 fromTABLE_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:..but you should use an
ORDER BY
as well or you'll get any randomb.importantfield
value.