db2:使用不同表上的选择更新多行和字段 [英] db2: update multiple rows and field with a select on a different table
本文介绍了db2:使用不同表上的选择更新多行和字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用不同表 (Bc Bd) 的值 c 和 d 为 A 的所有行(其中 Ax == Bz)增加表的字段 a 和 b(Aa 和 Ab)?
is it possible to increment the field a and b of a table (A.a and A.b) using the value c and d of a different table (B.c B.d) for all the row of A where A.x == B.z?
这个查询让我发疯了
推荐答案
DB2 和 SQL 标准在 UPDATE 语句中没有 FROM 子句.所以你必须清楚地将步骤分开来
DB2 and the SQL standard don't have a FROM clause in an UPDATE statement. So you have to clearly separate the steps to
- 确定要修改的行和要修改的行
- 计算新值.
.
这是一个例子:
UPDATE TABLE A
SET A.FLD_SUPV = ( SELECT B.FLD_SUPV
FROM TABLEA A, TABLEB B, TABLEC C,TABLED D
WHERE A.FLD1= B.FLD1
AND A.FLD_DT >= B.FLD_FM_DT
AND A.FLD_DT <= B.FLD_THRU_DT
AND A.FLD_DT > D.FLD_THRU_DT
AND A.FLD_DT < C.FLD_EFF_DT )
WHERE EXISTS ( SELECT B.FLD_SUPV
FROM TABLEA A, TABLEB B, TABLEC C,TABLED D
WHERE A.FLD1= B.FLD1
AND A.FLD_DT >= B.FLD_FM_DT
AND A.FLD_DT <= B.FLD_THRU_DT
AND A.FLD_DT > D.FLD_THRU_DT
AND A.FLD_DT < C.FLD_EFF_DT )
要更新两个字段,您可以使用如下示例:
To update two fields you may use an example like this:
UPDATE table1 t1
SET (col1, col2) = (
SELECT col3, col4
FROM table2 t2
WHERE t1.col8=t2.col9
)
优化器将看到 SET 和 FROM 子句中的子查询是相同的,它应该将它们合并到内部执行计划中.
The optimizer will see that the sub-queries in the SET and the FROM clause are identical and it should merge them in the internal execution plan.
这篇关于db2:使用不同表上的选择更新多行和字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文