根据另一个表的值更新字段 [英] Update Field based on another table's values
本文介绍了根据另一个表的值更新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有更优雅的方式来编写以下 Microsoft SQL Server 2008 命令?
Is there a more elegant way to write the following Microsoft SQL Server 2008 command?
UPDATE TableB
SET TableBField2=0
WHERE TableBID IN(
SELECT TableBID
FROM TableB
JOIN TableA on TableB.TableAID=TableA.TableAID
WHERE TableBField2 < 0
AND TableAField1 = 0
)
简单地说,我正在做的是根据连接表中字段的值更新表.我想知道我对 IN() 的使用是否被认为是低效的.
In plain speak, what I'm doing is updating a table based on the value of a field in a joined table. I wonder if my use of IN() is considered inefficient.
推荐答案
这样应该更有效率:
UPDATE TableB b
SET TableBField2=0
WHERE exists (
SELECT 1
FROM TableA
WHERE b.TableAID=TableA.TableAID
AND b.TableBField2 < 0
AND TableAField1 = 0
)
这篇关于根据另一个表的值更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文