如何在 Neo4j 数据库中找到每部电影收入最高的演员?

How to find an actor having max earnings per movie in Neo4j db?

我需要编写查询以在 Neo4j 数据库中查找每部电影收入最高的演员。

我写了下面的查询:

match (a:Person)-[role:ACTED_IN]->(m:Movie)
return m.title as Movie, max(role.earnings) as earnings

这给了我电影中的 max earning

如何找到那个最高收入对应的演员?

这里有两个选择。

再次匹配并寻找收入最高的actor/actors:

match (a:Person)-[role:ACTED_IN]->(m:Movie)
with m, max(role.earnings) as earnings
match (a:Person)-[:ACTED_IN{earnings:earnings]->(m)
return m.title as Movie, earnings, a.name as actor

排序并取出集合中的第一项:

match (a:Person)-[role:ACTED_IN]->(m:Movie)
with m, role.earnings as earnings, a.name as actor order by earnings
return m.title as Movie, collect(earnings)[0] as earnings, collect(actor)[0] as actor

需要考虑的其他一些问题:

  • 如果两个演员的收入相同,你想怎么处理?
  • 一个演员可以在同一个 movie/have 关系中扮演多个角色吗?ACTED_IN 关系?

您可以sort/collect按收入降序排列的电影、收入和演员。然后获取集合中的第一项。

MATCH (a:Person)-[role:ACTED_IN]->(m:Movie)
WITH m, role.earnings as earnings, a ORDER BY earnings DESC
RETURN m.title as Movie, collect(earnings)[0] as earnings, collect(a.name)[0] as actor