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

qltillow  于 2022-11-05  发布在  其他
关注(0)|答案(2)|浏览(174)

我需要写一个查询,在Neo4j数据库中找到每部电影收入最高的演员。
我在下面写了查询:

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

这就像电影中的max earning

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

rekjcdws

rekjcdws1#

这里有两种选择。
再次匹配并查找具有最大收入的演员:

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

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

  • 如果两个参与者的收入相同,您希望如何处理?
  • 一个演员是否可以在同一部电影中扮演多个角色/根据ACTED_IN关系获得收入?
eblbsuwk

eblbsuwk2#

你可以按收入降序对电影、收入和演员进行排序/收集。然后获取集合中的第一项。

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

相关问题