从另一个不带join语句的表中更新一个表(firebird) [英] Update one table from another w/o join statement (firebird)

查看:121
本文介绍了从另一个不带join语句的表中更新一个表(firebird)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据另一个表的值更新表中的列,我使用的Firebird 2.1版本稍旧,因此它在更新执行期间不支持join语句.为了消除这种情况,根据原始firebird常见问题解答中给出的说明, http://www.firebirdfaq.org/faq323 / 以下陈述应该可以使用,但是它会丢失某些值和该列的值,并且返回为null,如下面的表格形式的数据集所示.

I'd like to update the columns in on table based on the values of another table, I use a slightly old version of Firebird 2.1 so it doesn't have support for the join statement during update execution. In order to eliminate that, based on the instructions given in original firebird faqs http://www.firebirdfaq.org/faq323/ following statment should work, but it misses some of the values and values for that columns a returned as null as shown in the below tabular form of dataset.

例如,Elements表中的Num 21在其END_I列中的值应为23,因为它与节点表具有完全相同的X_I,Y_I和Z_I值,但是上面的语句返回null.

For example, Num 21 from Elements table should have 23 as value in its END_I column because it has exactly the same X_I, Y_I and Z_I values with node tables, but above statement 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))

ELEMENTS 

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


NODES 
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并不知道子查询仅返回一行.

You should add first 1 because of firebird 2.1 doestn`t know that subquery return only one row.

这篇关于从另一个不带join语句的表中更新一个表(firebird)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆