Sql server 2008 r2 如果两个不同表的列值匹配,则添加值

Sql server 2008 r2 如果两个不同表的列值匹配,则添加值,sql-server-2008-r2,Sql Server 2008 R2,我在SQLServer2008中有两个表A和表B。 两个表中都存在Id列 表A有一个新的列结果 问题是表B中ID列的所有值都与表A中ID列的值相匹配,那么我应该在表A的结果列中输入TRUE或false 提前谢谢 知道答案了!!如果表有1000万行,则不确定性能 我自己试了试,得到了答案 声明临时表 声明@temp表 (id int,result varchar) 插入到@temp中,选择A1.id,A1.A1从A1.id=B1.id上的A1内部连接B B1得到结果 更新@temp set res

我在SQLServer2008中有两个表A和表B。 两个表中都存在Id列 表A有一个新的列结果 问题是表B中ID列的所有值都与表A中ID列的值相匹配,那么我应该在表A的结果列中输入TRUE或false

提前谢谢

知道答案了!!如果表有1000万行,则不确定性能 我自己试了试,得到了答案

声明临时表

声明@temp表 (id int,result varchar)

插入到@temp中,选择A1.id,A1.A1从A1.id=B1.id上的A1内部连接B B1得到结果


更新@temp set result=true

您必须运行以下两个查询才能实现您的要求

update A set A.result = true from tableA A, tableB B where A.Id = B.Id
update A set A.result = false from tableA A, tableB B where A.Id != B.Id
试试这个

Update tableA a 
set result = true 
where exists (select 1 from tableB b where a.id=b.id);

Update tableA a 
set result = false
where not exists (select 1 from tableB b where a.id=b.id);

更新了答案。我之前给出的语法是针对MySQL的。你最好将自己的答案发布到你的问题上,而不是编辑问题以包含答案。它让其他用户看到结果。关于您的答案,它不会将任何内容设置为
false
,也不会更新
A
。我们是否误解了这个问题?
Update tableA a 
set result = true 
where exists (select 1 from tableB b where a.id=b.id);

Update tableA a 
set result = false
where not exists (select 1 from tableB b where a.id=b.id);