【发布时间】:2012-05-11 19:07:24
【问题描述】:
我有 3 个表,我需要通过计算其他两个表的数据来更新第三个表的列。
update table3 set column3=
(
select t2.column3+t1.column3
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id= 100
)
where id= 100;
这个查询工作正常,它会更新第三个表列,但是如果我提供这样的 IN 运算符:
update table3 set column3=
(
select t2.column3+t1.column3
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
)
where id IN (100,101);
这失败了,我收到了这条消息
子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。 声明已终止。
& 我知道这是因为子查询返回超过 1 行,我该如何处理这种情况?任何提示/想法都会有所帮助。
如何更新多个 ID? IE。 ID 100 返回的选择查询值应针对第三个表中的 ID 100 进行更新,对于 ID 101 也是如此。
另外,我需要像这样 sum(t2.column3)- (t1.column3 + t1.column2) 进行求和
update table3 set column3=
(
select sum(t2.column3)- (t1.column3 + t1.column2)
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
)
where id IN (100,101);
【问题讨论】:
-
你说得对,我不需要内部 id 条件。
标签: sql sql-server sql-server-2008 tsql sql-server-2005