【问题标题】:Neo4j; Complex cypher query新4j;复杂密码查询
【发布时间】:2020-02-06 15:19:12
【问题描述】:

我有以下neo4j结构:

:Label1 -[:rel-label-1]- :Label2
It may happen that :Label1 -- :Label3
And sometime :Label2 -[:rel-label-2]- :Label3

我想获取如下表结构的数据

| LABEL1 | LABEL2 |   
|--------|--------| 

但是这样

如果没有来自 Label2 节点的出站 :rel-label-2, 表中Lable 3为null,Label2节点只会出现在一行

如果没有来自 Label3 节点的入站 :rel-label-2, 表中Lable 2为空,Label3节点只会出现在一行

现在我只能根据结构中第一行的匹配生成笛卡尔积:

MATCH(n:Label1)--(:rel-label-1)--(z1:Label2)
Followed by the 2 optional Matches to retrieve :Label3 and :rel-label-2

请在下面找到图形和密码预期表格结果的示例

【问题讨论】:

  • 你能澄清你的 2 个IF 声明吗?也许用一些带注释的伪代码来写它们?
  • 根据要求,cybersam我添加了一个例子。

标签: neo4j cypher


【解决方案1】:

这是一种可能的解决方案。它将它分解为三个部分,将每个部分的结果保存在一个集合中,然后在最后返回它们。

// find the blue and yellow with a direct conneciton
MATCH (b:Blue)--(y:Yellow)
WITH [b.name, y.name] AS pair
// keep them in a collection of pairs
WITH collect(pair) AS pairs

// find the blues connected to alpha and not a yellow
MATCH (b:Blue)<--(:Alpha)
WHERE NOT (b)--(:Yellow)
// add them to the pairs collection
WITH pairs + [[b.name, null]] AS pairs

// find the yellows connected to alpha and not a blue
MATCH (y:Yellow)<--(:Alpha)
WHERE NOT (:Blue)--(y)
// add them to the pairs collection
WITH pairs + [[null, y.name]] AS pairs

// unwind the collection
UNWIND pairs AS pair
RETURN pair[0] AS blue, pair[1] AS yellow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2015-04-19
    相关资源
    最近更新 更多