在没有连接语句的情况下从另一个更新一个 table
Update one table from another without join statement
我想根据另一个 table 的值更新 table 中的列,我使用的是稍旧版本的 Firebird 2.1,因此它不支持更新执行期间的连接语句。为了消除这种情况,根据原始 Firebird 常见问题解答 http://www.firebirdfaq.org/faq323/ 中给出的说明
以下语句应该有效,但它遗漏了一些值和该列的值 a 返回为 null,如下面的数据集表格形式所示。
例如,元素 table 中的数字 21 在其 END_I 列中的值应为 23,因为它具有完全相同的 X_I、Y_I 和 Z_I 值与节点 tables,但上面的语句 returns null .
update elements E set E.END_I = (select n.node_num from nodes N
where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I) )
where exists (select 1 from nodes N where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I))
元素
Num End_I End_J X_I Y_I Z_I
17 18.000000 19.000000 0.000000 1.500000 18.000000 0.000000 1.500000 21.000000
18 19.000000 20.000000 0.000000 1.500000 21.000000 0.000000 1.500000 24.000000
19 20.000000 21.000000 0.000000 1.500000 24.000000 0.000000 1.500000 27.000000
20 21.000000 22.000000 0.000000 1.500000 27.000000 0.000000 1.500000 30.000000
21 [null] 24.000000 2.400000 0.000000 0.000000 2.400000 0.000000 3.000000
22 [null] 25.000000 2.400000 0.000000 3.000000 2.400000 0.000000 6.000000
23 [null] 26.000000 2.400000 0.000000 6.000000 2.400000 0.000000 9.000000
节点
Node_Num XI YI ZI
20 0.000000 1.500000 24.000000
21 0.000000 1.500000 27.000000
22 0.000000 1.500000 30.000000
23 2.400000 0.000000 0.000000
24 2.400000 0.000000 3.000000
25 2.400000 0.000000 6.000000
更新您的查询如下
update elements E set E.END_I = (select first 1 n.node_num from nodes N
where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I) )
where exists (select 1 from nodes N where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I))
您应该添加 first 1
因为 Firebird 2.1 不知道子查询 returns 只有一行。
与其尝试使用 UPDATE
,不如使用 MERGE
:
merge into elements E
using node N
on N.XI = E.X_I and N.YI = E.Y_I and N.ZI = E.Z_I
when matched then
update set E.END_I = N.node_num
Merge 允许您使用另一个 table、视图或查询作为数据源来更新或插入 table。
我想根据另一个 table 的值更新 table 中的列,我使用的是稍旧版本的 Firebird 2.1,因此它不支持更新执行期间的连接语句。为了消除这种情况,根据原始 Firebird 常见问题解答 http://www.firebirdfaq.org/faq323/ 中给出的说明 以下语句应该有效,但它遗漏了一些值和该列的值 a 返回为 null,如下面的数据集表格形式所示。
例如,元素 table 中的数字 21 在其 END_I 列中的值应为 23,因为它具有完全相同的 X_I、Y_I 和 Z_I 值与节点 tables,但上面的语句 returns null .
update elements E set E.END_I = (select n.node_num from nodes N
where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I) )
where exists (select 1 from nodes N where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I))
元素
Num End_I End_J X_I Y_I Z_I
17 18.000000 19.000000 0.000000 1.500000 18.000000 0.000000 1.500000 21.000000
18 19.000000 20.000000 0.000000 1.500000 21.000000 0.000000 1.500000 24.000000
19 20.000000 21.000000 0.000000 1.500000 24.000000 0.000000 1.500000 27.000000
20 21.000000 22.000000 0.000000 1.500000 27.000000 0.000000 1.500000 30.000000
21 [null] 24.000000 2.400000 0.000000 0.000000 2.400000 0.000000 3.000000
22 [null] 25.000000 2.400000 0.000000 3.000000 2.400000 0.000000 6.000000
23 [null] 26.000000 2.400000 0.000000 6.000000 2.400000 0.000000 9.000000
节点
Node_Num XI YI ZI
20 0.000000 1.500000 24.000000
21 0.000000 1.500000 27.000000
22 0.000000 1.500000 30.000000
23 2.400000 0.000000 0.000000
24 2.400000 0.000000 3.000000
25 2.400000 0.000000 6.000000
更新您的查询如下
update elements E set E.END_I = (select first 1 n.node_num from nodes N
where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I) )
where exists (select 1 from nodes N where (N.XI =E.X_I and N.YI = E.Y_I and N.ZI=E.Z_I))
您应该添加 first 1
因为 Firebird 2.1 不知道子查询 returns 只有一行。
与其尝试使用 UPDATE
,不如使用 MERGE
:
merge into elements E
using node N
on N.XI = E.X_I and N.YI = E.Y_I and N.ZI = E.Z_I
when matched then
update set E.END_I = N.node_num
Merge 允许您使用另一个 table、视图或查询作为数据源来更新或插入 table。