简体   繁体   English

Neo4j:查询以找到最多的 2 个节点

[英]Neo4j: query to find 2 node with the most

I'm trying to find which pair of actors have acted together in most number of movies in my data base and my query kept returning blank, any suggestions?我试图找出哪一对演员在我的数据库中的大多数电影中一起表演过,但我的查询一直返回空白,有什么建议吗?

MATCH (actor1:Actor)<-[st1:ACTED_IN]-(mv1:Movie)-[st2:ACTED_IN]->(actor2:Actor) 
RETURN distinct actor1,actor2,count(mv1)

Looks like you have written relationship arrows in reverse direction.看起来你写了相反方向的关系箭头。

It might be from Actor to Movie:它可能是从演员到电影:

MATCH (actor1:Actor)-[st1:ACTED_IN]->(mv1:Movie)<-[st2:ACTED_IN]-(actor2:Actor) 
RETURN distinct actor1, actor2, count(mv1)

Though you are using distinct your query will return duplicates because the following two are different records:尽管您使用的是 distinct 您的查询将返回重复项,因为以下两个是不同的记录:

actor1, actor2, movie_count  
actor2, actor1, movie_count

To get rid of this duplicate entries you can use simple trick of comparing ids of nodes like:要摆脱这些重复条目,您可以使用比较节点 ID 的简单技巧,例如:

MATCH (actor1:Actor)-[:ACTED_IN]->(mv1:Movie)<-[:ACTED_IN]-(actor2:Actor) 
WHERE id(actor1)>id(actor2)
RETURN actor1,actor2,count(mv1)

To find actors acted in most movies:要查找在大多数电影中出演的演员:

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.

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