通过过滤链接集合的多个字段来选择父记录

时间:2016-02-28 21:24:56

标签: orientdb

我一直试图弄清楚这几天知道,但我无法想出一个能给我正确结果的查询。这个任务的本质是我试图检索一个图形的所有节点,这些节点具有满足多个约束的属性的子节点。我遇到的问题是节点可能有多个链接节点,当我尝试应用条件来限制查询必须返回哪些节点时,需要对节点集而不是单个节点施加标准。

让我通过一个例子更详细地解释这个问题。以下是公司和地点的示例模式。每家公司都可以有多个地点。

create class company extends V;
create property company.name STRING;

create class location extends V;
create property location.name STRING;
create property location.type INTEGER;
create property location.inactive STRING;

现在让我创建一些记录来说明我遇到的问题。

create vertex company set name = 'Company1';
create vertex location set name = 'Location1', type = 5;
create vertex location set name = 'Location2', type = 7;
create edge from (select from company where name = 'Company1') to (select from location where name in ['Location1', 'Location2']);

create vertex company set name = 'Company2';
create vertex location set name = 'Location3', type = 6;
create vertex location set name = 'Location4', type = 5, inactive = 'Y';
create edge from (select from company where name = 'Company2') to (select from location where name in ['Location3','Location4']);

我想检索所有没有类型5位置或类型5处于非活动状态(非活动='Y')的公司。我最初尝试的查询如下所示。它不起作用,因为$ loc.type是针对值集合而不是单个记录计算的,因此对于每个位置记录的单个字段“inactive”而不是针对字段值的集合应用null每个父记录都“不活动”。我尝试了子查询,set函数,追加等等,但我无法让它工作。

select from company let loc = out() where $loc.type not contains 5 or ($loc.type contains 5 and $loc.type is null)

2 个答案:

答案 0 :(得分:0)

尝试此查询:

select expand($d) from company
let $a=(select from company where out().type <> 5 and name contains $parent.current.name),
$b=(select from company where out().type contains 5 and name=$parent.current.name),
$c=(select from company where out().inactive contains "Y" and name=$parent.current.name),
$d=unionall($a,intersect($b,$c))

希望它有所帮助,

此致

MICHELA

答案 1 :(得分:0)

您可以尝试使用此查询:

select expand($c)
let $a = ( select expand(out) from E where out.@class = "company" and in.@class="location" and in.type = 5 and in.inactive = "Y" ),
    $b = ( select from company where 5 not in out("E").type ),
    $c = unionAll($a,$b)

更新

我已创建此图

enter image description here

您可以使用此查询

select expand($f)
let $a = ( select from E where out.@class = "company" and in.@class="location" ),
    $b = ( select expand(out) from $a where in.type = 5 and in.inactive = "Y"),
    $c = ( select expand(out) from $a where in.type = 5 and in.inactive is null),
    $d = difference($b,$c),
    $e =  ( select from company where 5 not in out("E").type ),
    $f = unionAll($d,$e)

enter image description here

希望它有所帮助。