匹配两个或多个关系(AND)关系的Neo4j节点的表达

时间:2018-12-12 10:32:25

标签: neo4j

enter image description here我需要一个表达式来匹配满足两个或更多关系的Neo4j节点。我尝试了以下方法:

MATCH (p:pm{name: 'G1'})-[:HAS]->(e:en{name:'GREY1'})-[:WITH]-(a:RED2),
      (p:pm{name: 'G1'})-[:HAS]->(e:en{name:'GREY2'})-[:WITH]-(a:RED2) 
RETURN a

MATCH (p)--(a)--(e) 
WHERE (p:pm{name: 'G1'})-[:HAS]->(e:en{name:'GREY1'})-[:WITH]-(a:RED2) AND
      (p:pm{name: 'G1'})-[:HAS]->(e:en{name:'GREY2'})-[:WITH]-(a:RED2) 
RETURN a

但是我没有返回任何节点,两个match语句都是单独工作的

[如何致电RED2]

2 个答案:

答案 0 :(得分:1)

问题是您将相同的变量名称e用于不同的节点。试试这个:

MATCH (p:pm{name: 'G1'})-[:HAS]->(e1:en{name:'GREY1'})-[:WITH]-(a:RED2),
      (p:pm{name: 'G1'})-[:HAS]->(e2:en{name:'GREY2'})-[:WITH]-(a:RED2) 
RETURN a

答案 1 :(得分:0)

此查询将为您提供以下响应:

MATCH (p:pm{name: 'G1'})-[:HAS]->(e1:en{name:'GREY1'})-[:WITH]-(a:RED2),
      (p:pm{name: 'G1'})-[:HAS]->(e2:en{name:'GREY2'})-[:WITH]-(a:RED2) 
      RETURN a

这是流程:

通过关系HAS从G1节点到GREY1和GREY2,现在您已经到达那些节点,任何与GREY1和GREY2具有WITH关系的RED2节点都将作为返回。

如果您需要保留G1与RED2的关系,则还必须在查询中包括TO关系。

所以您的查询应该是:

  `MATCH (p:pm{name: 'G1'})-[:HAS]->(e1:en{name:'GREY1'})-[:WITH]-(a:RED2),
  (p:pm{name: 'G1'})-[:HAS]->(e2:en{name:'GREY2'})-[:WITH]-(a:RED2), 
  (P:pm{name:'G1'})-[:TO]->(a:RED2)
  RETURN a`