在 Neo4j Cypher 的 RETURN 语句中使用 WITH 语句之前的变量
Use vars from before WITH statement in RETURN statement in Neo4j Cypher
我开始使用 Neo4j (v2.1.5),但我遇到了以下 Cypher 查询的问题:
MATCH (actor:Person{name:"Tom Cruise"})-[role:ACTED_IN]->(movies)<-[r:ACTED_IN]-(coactors)
WITH coactors, count(coactors) as TimesCoacted
RETURN coactors.name, avg(TimesCoacted)
ORDER BY avg(TimesCoacted) DESC
它基于 Neo4j 安装附带的迷你电影图。
一切正常,它显示了与汤姆·克鲁斯在电影中合作的所有合作演员,以及他们合作了多少次,但是当我想列出他们合作过的电影时,问题就出现了。在 RETURN 语句中放置 'movies' 变量会引发以下错误:
movies not defined (line 3, column 9)
"RETURN movies, coactors.name, avg(TimesCoacted)"
^
有什么方法可以在一次查询中完成吗?
尝试以下操作:
MATCH
(actor:Person{name:"Tom Cruise"})-[role:ACTED_IN]->(movies)<-[r:ACTED_IN]-(coactors)
WITH
coactors,
count(coactors) as TimesCoacted,
movies // You have declare "movies" here in order to use it later in the query
RETURN
movies,
coactors.name,
avg(TimesCoacted)
ORDER BY
avg(TimesCoacted) DESC
您在 WITH
语句中定义的内容是唯一可用于进一步处理的内容。在最初的问题中,movies
没有进入下一节(它不是 WITH
的一部分),因此 movies
不能在 return 语句中使用。
编辑:从 OP 更新后添加了以下内容。
再举个例子。如果您想计算演员在一部电影中 合作 的次数,并列出 movie-titles。尝试以下操作:
MATCH
(actor:Person {name:"Tom Cruise"})-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(coactor:Person)
WITH
actor,
coactor,
collect (distinct movie.title) as movieTitles
RETURN
actor.name as actorName,
coactor.name as coactorName,
movieTitles,
size(movieTitles) as numberOfMovies
MATCH
(actor:Person{name:"Tom Cruise"})-[role:ACTED_IN]->(movies)<-[r:ACTED_IN]-(coactors)
WITH
coactors,
count(coactors) as TimesCoacted,
collect(DISTINCT movies.title) as movies // <=- this line was crucial!
RETURN
movies,
coactors.name,
avg(TimesCoacted)
ORDER BY
avg(TimesCoacted) DESC
我开始使用 Neo4j (v2.1.5),但我遇到了以下 Cypher 查询的问题:
MATCH (actor:Person{name:"Tom Cruise"})-[role:ACTED_IN]->(movies)<-[r:ACTED_IN]-(coactors)
WITH coactors, count(coactors) as TimesCoacted
RETURN coactors.name, avg(TimesCoacted)
ORDER BY avg(TimesCoacted) DESC
它基于 Neo4j 安装附带的迷你电影图。
一切正常,它显示了与汤姆·克鲁斯在电影中合作的所有合作演员,以及他们合作了多少次,但是当我想列出他们合作过的电影时,问题就出现了。在 RETURN 语句中放置 'movies' 变量会引发以下错误:
movies not defined (line 3, column 9)
"RETURN movies, coactors.name, avg(TimesCoacted)"
^
有什么方法可以在一次查询中完成吗?
尝试以下操作:
MATCH
(actor:Person{name:"Tom Cruise"})-[role:ACTED_IN]->(movies)<-[r:ACTED_IN]-(coactors)
WITH
coactors,
count(coactors) as TimesCoacted,
movies // You have declare "movies" here in order to use it later in the query
RETURN
movies,
coactors.name,
avg(TimesCoacted)
ORDER BY
avg(TimesCoacted) DESC
您在 WITH
语句中定义的内容是唯一可用于进一步处理的内容。在最初的问题中,movies
没有进入下一节(它不是 WITH
的一部分),因此 movies
不能在 return 语句中使用。
编辑:从 OP 更新后添加了以下内容。
再举个例子。如果您想计算演员在一部电影中 合作 的次数,并列出 movie-titles。尝试以下操作:
MATCH
(actor:Person {name:"Tom Cruise"})-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(coactor:Person)
WITH
actor,
coactor,
collect (distinct movie.title) as movieTitles
RETURN
actor.name as actorName,
coactor.name as coactorName,
movieTitles,
size(movieTitles) as numberOfMovies
MATCH
(actor:Person{name:"Tom Cruise"})-[role:ACTED_IN]->(movies)<-[r:ACTED_IN]-(coactors)
WITH
coactors,
count(coactors) as TimesCoacted,
collect(DISTINCT movies.title) as movies // <=- this line was crucial!
RETURN
movies,
coactors.name,
avg(TimesCoacted)
ORDER BY
avg(TimesCoacted) DESC