如何在Cypher查询中使用两个匹配语句

9
我想将两个请求合并成一个查询,但我不确定在单个cypher查询中使用两个match语句会发生什么。
假设我有一个朋友列表,我想看到我的朋友列表,并列出他们每个人的叔叔和兄弟姐妹。我可以有两个match语句来完成这个工作吗?例如:
match friends-[:childOf]->parents-[:brother]->uncles
    , friends-[:childOf]->parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)

然而,如果我进行此类查询,它总是返回无结果。

2
你能否通过http://console.neo4j.org/分享一个图形示例? - Thomas Fenzl
2个回答

10

由于您已经在第一选择阶段选择了父母,因此您可以像这样操作 -

match friends-[:childOf]->parents-[:brother]->uncles
with friends, parents, uncles
match parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)

match friends-[:childOf]->parents-[:brother]->uncles, parents<-[:childOf]-siblings return friends, collect(siblings), collect(uncles) 不就是基本相同的吗? - MrE
相同。WITH 就像“软” RETURN,即 WITH 影响作用域内的变量。未包含在 WITH 子句中的任何变量都不会传递到查询的其余部分。通配符 * 可用于包括当前作用域内的所有变量。因此,如果没有 WITH,则所有变量仍然被定义。 - palandlom

1

您可能希望将其中一些关系设置为可选。例如,如果您找到了一个兄弟但没有找到任何叔叔,此查询将返回null,因为它未满足两个匹配子句。如果您使最终关系成为可选,则不必完全满足两个子句即可返回数据。因此:

match friends-[:childOf]->parents-[?:brother]->uncles
    , friends-[:childOf]->parents<-[?:childOf]-siblings
return friends, collect(siblings), collect(uncles)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接