简体   繁体   English

如何在 Neo4j Cypher 中返回不同的节点对?

[英]How to return distinct node-pair in Neo4j Cypher?

I have Node type: Person and Neighborhood.我有节点类型:人和邻居。 When trying to find the persons LIVES in the same neighborhood, I have the persons appeared twice, in different order.当试图找到同一街区的人时,我让这些人以不同的顺序出现了两次。

MATCH (p1:Person)-[n1:LIVES]-(n:Neighborhood)-[n2:LIVES]-(p2:Person)  
RETURN n.name, p1.name, p2.name

returns:返回:

"Riverdale", "Paul", "James"
"Riverdale", "Paul", "Mary"
"Riverdale", "James", "Paul"
"Riverdale", "Mary", "Paul"
"Newton", "Zoe", "Harry"
"Newton", "Harry", "Zoe"
...

How can I deduplicate the result set to have the pair appear only once?如何对结果集进行重复数据删除以使该对只出现一次?

"Riverdale", "Paul", "James"
"Riverdale", "Paul", "Mary"
"Newton", "Zoe", "Harry"
...

I tried DISTINCT but it did not work as the list is ordered.我尝试了 DISTINCT 但它没有工作,因为列表是有序的。

The pattern matches both ways so to eliminate one half of the responses you can add a WHERE clause that specifies the id() of one of the Person nodes is greater than the other.该模式匹配两种方式,因此为了消除一半的响应,您可以添加一个WHERE子句,该子句指定Person节点之一的id()大于另一个。

MATCH (p1:Person)-[n1:LIVES]-(n:Neighborhood)-[n2:LIVES]-(p2:Person)  
WHERE id(p1) > id(p2)
RETURN n.name, p1.name, p2.name

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM