Beginner in Cypher: List actors that acted in all 3 Matrix movies

点点圈 提交于 2019-12-11 01:16:21

问题


I started learning Neo4j last friday.

I've completed the Cypher examples on my local database, are there are some additional queries that I'd like to learn how to ask:

Given the three matrix movies, how do I list only actors that acted in all three of them? Or more generally, suppose I have a bunch of movies, actors and ACTS_IN relationships.

Given 3 movies:

  • List all the actors that acted in ALL 3
  • List all the actors that acted in any of those 3 movies (and which movies they acted in)

Likewise, given 3 actors

  • List all movies that have all 3 actors
  • List all movies that have any one of those 3 actors (and which actors they have)

Thanks!!


回答1:


acted in all 3:

start m1=node:node_auto_index(title="The Matrix"),
      m2=node:node_auto_index(title="The Matrix Reloaded"),
      m3=node:node_auto_index(title="The Matrix Revolutions")
match a-[:ACTS_IN]->m1, a-[:ACTS_IN]->m2, a-[:ACTS_IN]->m3
return a;

acted in any: (easiest syntax is the union only from 2.0, so I'm going to go with that)

start m=node:node_auto_index(title="The Matrix")
match a-[:ACTS_IN]->m
return a, m
union
start m=node:node_auto_index(title="The Matrix Reloaded")
match a-[:ACTS_IN]->m
return a, m
union
start m=node:node_auto_index(title="The Matrix Revolutions")
match a-[:ACTS_IN]->m
return a, m;

And they're basically the same for the other two queries, just switch title for name, and put the people's names, and swap m for a in the start clause. :)



来源:https://stackoverflow.com/questions/18964315/beginner-in-cypher-list-actors-that-acted-in-all-3-matrix-movies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!