Cypher/Neo4j –在全局范围内存储括号内的变量

发布于 2025-01-12 13:31:36 字数 363 浏览 0 评论 0原文

以下密码查询在最后一行失败,因为 p2 仅在 EXISTS 之后的范围内定义,但是有没有办法以某种方式将其带出该范围(例如将其设置为到一个全局变量)并返回它?

MATCH (p:Person)
WHERE EXISTS {
  MATCH (p{name: 'José'})<-[:CHILD_OF]-(p2:Person)
  WHERE p2.name IN ['Roberto', 'Gustavo']  
}
RETURN (p), (p2) // fails

PS:我知道最简单的解决方案就是首先不使用范围,我的问题是我正在寻找的功能是否存在。

The following cypher query fails at the last line because p2 is only defined within scope after EXISTS, but is there a way to bring it out of that scope somehow (e.g. set it to a global variable) and return it?

MATCH (p:Person)
WHERE EXISTS {
  MATCH (p{name: 'José'})<-[:CHILD_OF]-(p2:Person)
  WHERE p2.name IN ['Roberto', 'Gustavo']  
}
RETURN (p), (p2) // fails

P.S.: I know that the easiest solution is to just NOT use the scope in the first place, my question is whether the functionality I'm looking for exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放赐 2025-01-19 13:31:36

EXISTS 中使用的存在查询无法返回值。如果您想对子查询的结果进行后处理,则需要使用CALL
看起来您想尝试:

MATCH (p:Person)
CALL {
  WITH p
  MATCH (p{name: 'José'})<-[:CHILD_OF]-(p2:Person)
  WHERE p2.name IN ['Roberto', 'Gustavo'] 
  RETURN p2 
}
RETURN p, p2

CALL 中的WITH 使外部查询中的p 可用于内部查询。

Existential queries used in EXISTS cannot return values. You need to use CALL if you want to do post processing of a sub query's results.
It looks like you want to try:

MATCH (p:Person)
CALL {
  WITH p
  MATCH (p{name: 'José'})<-[:CHILD_OF]-(p2:Person)
  WHERE p2.name IN ['Roberto', 'Gustavo'] 
  RETURN p2 
}
RETURN p, p2

The WITH in the CALL makes the p from the outer query available to the inner query.

唔猫 2025-01-19 13:31:36

我会这样做:

MATCH (p:Person)<-[:CHILD_OF]-(p2:Person)
WHERE p.name = 'Josè'
AND p2.name IN ['Roberto', 'Gustavo']
RETURN p,p2

I would have done this:

MATCH (p:Person)<-[:CHILD_OF]-(p2:Person)
WHERE p.name = 'Josè'
AND p2.name IN ['Roberto', 'Gustavo']
RETURN p,p2
哆啦不做梦 2025-01-19 13:31:36

您可以删除 where contains 子句,直接对 Jose 和他的父母进行查询。

MATCH (p: Person{name: 'José'})<-[:CHILD_OF]-(p2:Person)
WHERE p2.name IN ['Roberto', 'Gustavo']  
RETURN p, p2

You can remove the where exists clause and directly do the query on Jose and his parents.

MATCH (p: Person{name: 'José'})<-[:CHILD_OF]-(p2:Person)
WHERE p2.name IN ['Roberto', 'Gustavo']  
RETURN p, p2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文