![](/img/trans.png)
[英]Cypher query in neo4j to find specific node with most paths matching pattern
[英]Neo4j: query to find 2 node with the most
我试图找出哪一对演员在我的数据库中的大多数电影中一起表演过,但我的查询一直返回空白,有什么建议吗?
MATCH (actor1:Actor)<-[st1:ACTED_IN]-(mv1:Movie)-[st2:ACTED_IN]->(actor2:Actor)
RETURN distinct actor1,actor2,count(mv1)
看起来你写了相反方向的关系箭头。
它可能是从演员到电影:
MATCH (actor1:Actor)-[st1:ACTED_IN]->(mv1:Movie)<-[st2:ACTED_IN]-(actor2:Actor)
RETURN distinct actor1, actor2, count(mv1)
尽管您使用的是 distinct 您的查询将返回重复项,因为以下两个是不同的记录:
actor1, actor2, movie_count
actor2, actor1, movie_count
要摆脱这些重复条目,您可以使用比较节点 ID 的简单技巧,例如:
MATCH (actor1:Actor)-[:ACTED_IN]->(mv1:Movie)<-[:ACTED_IN]-(actor2:Actor)
WHERE id(actor1)>id(actor2)
RETURN actor1,actor2,count(mv1)
要查找在大多数电影中出演的演员:
MATCH (actor1:Actor)-[:ACTED_IN]->(mv1:Movie)<-[:ACTED_IN]-(actor2:Actor)
WHERE id(actor1)>id(actor2)
RETURN actor1,actor2,count(mv1) AS movie_count
ORDER BY movie_count DESC
LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.