简体   繁体   English

查找通过 2 个关系连接到至少 2 个相同节点但中间节点不同的节点

[英]Finding nodes which are connected to at least 2 same nodes through 2 relationships but with the intermediary one being different

I would like to find out all the couples of Persons and Accidents in the following case:我想找出以下案例中的所有人员和事故夫妇:

  • Persons who are involved into at least 2 same Accidents, when they were in 2 different Cars.参与至少 2 起相同事故的人,当他们乘坐 2 辆不同的汽车时。

I always have the following relationships: Person --> Car -- > Accident我总是有以下关系:人 --> 车 --> 事故

I made a simplified and reproducible example:我做了一个简化且可重现的例子:

CREATE
  (p1:Person {name: 'Paul'})-[:DRIVES]->(c1:Car {name: 'Car A'}),
  (p2:Person {name: 'John'})-[:DRIVES]->(c2:Car {name: 'Car B'}),
  (p3:Person {name: 'Mike'})-[:DRIVES]->(c3:Car {name: 'Car C'}),
  (p4:Person {name: 'Joe'})-[:OCCUPANT]->(c1),
  (p4)-[:OCCUPANT]->(c3),
  (c1)-[:IS_DOER]->(a1: Accident {name: 'Crash 1'}),
  (c2)-[:IS_VICTIM]->(a1),
  (c2)-[:IS_DOER]->(a2: Accident {name: 'Crash 2'}),
  (c3)-[:IS_VICTIM]->(a2) ;

And here is what the graph looks like:这是图表的样子:

在此处输入图片说明

In this example, I would like to return the couples ("Joe", "John") and ("Crash 1", "Crash 2"), because Joe and John are both involved into at least 2 same accidents while being in different cars.在这个例子中,我想返回这对夫妇 ("Joe", "John") 和 ("Crash 1", "Crash 2"),因为 Joe 和 John 都卷入了至少 2 起相同的事故,同时处于不同的汽车。

Thank you for your help.感谢您的帮助。

Actually I think I found the answer:其实我想我找到了答案:

MATCH
  (p1:Person)-[r1]->(c1:Car)-[r2]->(a:Accident)<-[r3]-(c2:Car)<-[r4]-(p2:Person)
WHERE c1<>c2
WITH p1, p2, COUNT(DISTINCT a) AS count_accident
WHERE count_accident>= 2
RETURN p1, p2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 匹配连接到同一中介的节点-cypher / neo4j - Match nodes connected to same intermediary - cypher/neo4j 查找具有2个关系的节点 - Finding nodes with 2 relationships 在具有不同属性的节点之间创建相同的关系 - Create same relationships between nodes with different properties 相同类型的多个关系,但在相同的两个节点之间具有不同的属性 - Multiple relationships of the same type but with different properties between the same two nodes Neo4j Cypher Query:查找连接到一个节点且具有 3 个以上其他关系的所有节点 - Neo4j Cypher Query: Finding all nodes, that are connected to a node, that has more than 3 other relationships Cypher:找到连接父节点的节点关系 - Cypher: Find relationships of nodes with connected parents cypher:合并具有相同属性和不同关系的两个节点 - cypher: merge two nodes with same attributes and different relationships Neo4j:查询查找具有最多关系的节点及其连接的节点 - Neo4j: Query to find the nodes with most relationships, and their connected nodes 如何在一个事务中的不同节点之间添加多个关系 - How to add multiple relationships between different nodes in one transaction Neo4j 如何获得(DISTINCT)所有节点以及与至少一个节点共同的关系? - Neo4j how to get (DISTINCT) all nodes and relationships with at least one node in common?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM