问题描述
我有两个具有关联的表格,并且我想更新表A中的一个字段.是否可以将更新和联接合并到同一查询中?我用谷歌搜索,但没有找到任何可行的解决方案?
I have two tabels with a relation and I want to update a field in table A. Is it possible to combine update and join in the same query? I googled it but didnt find any working solution?
UPDATE md SET md.status = '3'
FROM pd_mounting_details AS md
LEFT OUTER JOIN pd_order_ecolid AS oe ON md.order_data = oe.id
我正在使用MS SQL
I'm using MS SQL
推荐答案
Update t
SET
t.Column1=100
FROM
myTableA t
LEFT JOIN
myTableB t2
ON
t2.ID=t.ID
将myTableA
替换为表名,并将Column1
替换为列名.
Replace myTableA
with your table name and replace Column1
with your column name.
此后,只需将LEFT JOIN移至tableB.在这种情况下,t
只是myTableA
的别名. t2
是联接表的别名,在我的示例中为myTableB
.如果您不喜欢使用t
或t2
,请使用您喜欢的任何别名-没关系-我只是喜欢使用那些别名.
After this simply LEFT JOIN to tableB. t
in this case is just an alias for myTableA
. t2
is an alias for your joined table, in my example that is myTableB
. If you don't like using t
or t2
use any alias name you prefer - it doesn't matter - I just happen to like using those.
这篇关于更新和左外部联接语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!