合并两个Cypher查询结果

huangapple go评论67阅读模式
英文:

Combining two results in cypher

问题

我试图将两个MATCH语句的结果合并在一起。
例如像这样

MATCH (a)-[:connection]-(b:labelB)
WHERE a.id IN $selection
WITH COLLECT(a) + COLLECT(b) AS selection

然而,这样做的问题是它生成了一个类型为List<node>的列表,而不是node。这是一个问题,因为它不允许在另一个MATCH语句中使用selection
这意味着以下查询无法继续进行:

MATCH (selection)-[]-(c)
RETURN c

这种行为可以通过使用UNION来实现,但是当在第二个MATCH中使用第一个MATCH的结果时,它似乎会产生意外的行为。

MATCH (a)
WHERE a.id IN $selection
RETURN a AS selection
UNION
MATCH (a)-[]-(b)
RETURN b AS selection

如何将两个节点合并为单个值?

英文:

I am trying to combine the result of two MATCH statements.
for example like this

MATCH (a)-[:connection]-(b:labelB)
WHERE a.id IN $selection
WITH COLLECT(a) + COLLECT(b) AS selection

However, the issue with this is that it produces a list of type List<node> instead of node. This is an issue because it does not enable another MATCH statement using selection.
Meaning this would not be possible as a continuation to the query:

MATCH (selection)-[]-(c)
RETURN c

This behavior can be achieved by a UNION but it seems to behave unexpectedly when using the result of the first MATCH in the second MATCH.

MATCH (a)
WHERE a.id IN $selection
RETURN a AS selection
UNION
MATCH (a)-[]-(b)
RETURN b AS selection

How can I combine two Nodes into a single value?

答案1

得分: 1

你可以使用UNWIND,它类似于SQL中的WHILE循环。

MATCH (a)-[:connection]-(b:labelB)
WHERE a.id IN $selection
WITH COLLECT(a) + COLLECT(b) AS selections
UNWIND selections as selection
MATCH (selection)-[]-(c)
RETURN c
英文:

You can use UNWIND which is similar to a WHILE looping in sql.

MATCH (a)-[:connection]-(b:labelB)
WHERE a.id IN $selection
WITH COLLECT(a) + COLLECT(b) AS selections
UNWIND selections as selection
MATCH (selection)-[]-(c)
RETURN c

答案2

得分: 1

你也可以只用以下方式替换这部分代码:

MATCH (s)--(c)
WHERE s IN selection

顺便说一下,我应该指出,如果可能的话,你的第一个查询会导致selection中出现重复的节点,因为一个connection可以连接两个labelB节点。

英文:

You could also just replace this:

MATCH (selection)-[]-(c)

with this:

MATCH (s)--(c)
WHERE s IN selection

By the way, I should point out that your first query will cause selection to have duplicate nodes if it is possible for a connection to connect 2 labelB nodes.

huangapple
  • 本文由 发表于 2023年6月15日 20:50:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定