使用:通过 OrientDB Studio 附带的 OrientDB 3.0.4 ,带有官方 OrientDB docker镜像
如何在子查询let
中使用where
变量?
给出一个具有2个值的测试类:
insert into test content {a: 1};
insert into test content {a: 2};
测试:
select a from test;
返回:
a
1
2
使用let
变量的另一项测试按预期工作:
select a, $b from test let $b = a;
返回:
a $b
1 1
2 2
现在测试在let中使用子查询选择
select a, $b, $c from test let $b = a, $c = (select a from test);
也可以按预期工作并返回:
a $b $c
1 1 [{"a":1},{"a":2}]
2 2 [{"a":1},{"a":2}]
现在有问题的部分:
如何在$b
的子选择中使用变量$c
?
所有这些嵌套选择均不返回任何内容:
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parrent.a);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parrent.$b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parrent.b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parent.$current.$b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parent.$current.b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parent.current.$b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parent.current.b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $current.$b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = $current.b);
select a, $b, $c from test let $b = a, $c = (select a from test where a = b);
结果:
a $b $c
1 1 []
2 2 []
我在所有站点/论坛/官方文档的子查询中发现了用于访问let变量的所有这些变体,但实际上没有任何作用。甚至来自https://orientdb.com/docs/3.0.x/sql/SQL-Query.html#let-block的官方示例也不起作用,并且失败了,com.orientechnologies.orient.core.exception.OCommandExecutionException: Class $parent not found DB name="test"
SELECT FROM Document LET $temp = ( SELECT @rid, $depth FROM (TRAVERSE
V.OUT, E.IN FROM $parent.current ) WHERE @class = 'Concept' AND
( id = 'first concept' OR id = 'second concept' )) WHERE $temp.SIZE() > 0;
起初,我认为该变量不会传播到嵌套选择,但这不是事实。使用此选择:
select a, $b, $c from test let $b = a, $c = (select a from test where a = $parrent.current.a);
嵌套选择返回非空列表
a $b $c
1 1 [{"a":1},{"a":2}]
2 2 [{"a":1},{"a":2}]
问题在于嵌套选择中的条件始终为true。看起来条件中的a
的值被$parent.a
的值覆盖,但a
语句中的select
的值却被覆盖(不返回相同的值a
和嵌套a
)
要覆盖select语句的值,只需在嵌套查询之外声明$a
:
select $b let $a = 1, $b = (select a, $parent.$a from test where a = $parent.$a);
返回:
[{"a":1,"$parent.$a":1},{"a":1,"$parent.$a":1}]
不使用相同的变量名将返回更好的结果:
select $b let $c = 4, $b = (select a, $parent.$c, c from test where c = $parent.$c);
a
仍具有原始值,但嵌套对象中不存在的c
保留$parent.$c
的值
[{"a":1,"$parent.$c":4,"c":4},{"a":2,"$parent.$c":4,"c":4}]
我做错什么了吗?还是我缺少let
变量概念中的某些内容,或者不了解可见性范围?还是查询引擎坏了?