在我创建的列表上使用UNWIND以返回多个值(密码)

时间:2019-11-13 15:39:12

标签: neo4j cypher

我正在使用Neo4j中的“电影”数据库来简化我的问题(在空沙箱的查询框中键入:play movies)。对于我指定的3个演员的列表,我想确定他们正在制作的电影总数,他们参加的电影数量以及所导演的电影数量(如果有)。这是我想出的:

MATCH (p:Person)-->(m:Movie)
WITH p, m, count(m) AS total
MATCH (p)-[:ACTED_IN]->(m)
WITH p, m, total, count(DISTINCT m) AS actedIn
MATCH (p)-[:DIRECTED]->(m)
WITH p, m, total, actedIn, count(DISTINCT m) AS directed
UNWIND ["Tom Hanks", "Clint Eastwood", "Charlize Theron"] AS actors
RETURN DISTINCT actors, total, actedIn, directed

目前,正在重新调整每个演员在1部电影中演出并执导1部电影,这是不正确的。我需要在查询中保留WITH子句,并且需要定义参与者列表。

在我正在处理的实际查询中,与这个简单查询相比,同一件事发生在我定义的列表的每个元素返回与列表中其他元素相同的数字的地方。我不确定我在做什么错。

1 个答案:

答案 0 :(得分:1)

我认为该查询将为您服务。

由于每个人都以某种身份参与了一部电影,因此第一个MATCH可以断言,然后随后的MATCH就可以是可选的。

// Find the people that worked in total movies controlled by your list
MATCH (p:Person)-->(m:Movie)
WHERE p.name IN ["Tom Hanks", "Clint Eastwood", "Charlize Theron"]

// carry the people and the total movies per person
WITH p, count(m) AS total

// find the movies those people acted in
OPTIONAL MATCH (p)-[:ACTED_IN]->(m:Movie)

// carry the people, total movies and the movies acted in
WITH p, total, count(m) AS actedIn

// find the movies they directed
OPTIONAL MATCH (p)-[:DIRECTED]->(m:Movie)
RETURN p.name, total, actedIn, count(m) AS directed