英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论