在没有找到匹配的可选匹配之后创建 [英] CREATE after OPTIONAL MATCH where no match was found

查看:22
本文介绍了在没有找到匹配的可选匹配之后创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个查询,如果另一个关系已经存在,它将创建一些关系.

I'm trying to write a query that will create some relationships if another relationship already exists.

START a=node(1), b=node(2), c=node(3)
OPTIONAL MATCH a-[r1:RELATIONSHIP]-(optional1)
OPTIONAL MATCH b-[r2:RELATIONSHIP]-(optional2)
CREATE c-[:NEW_RELATIONSHIP]->(optional1)
CREATE c-[:NEW_RELATIONSHIP]->(optional2)
DELETE r1, r2
RETURN a, b, c

查询返回错误:

期望 optional1 到一个节点,但它是一个空值"

"Expected optional1 to a node, but it is a null"

如果现有关系存在,有没有办法创建新关系?否则就忽略创建?

Is there a way to create the new relationship if the existing relationship exists? Otherwise just ignore the create?

推荐答案

现在,您可以使用 FOREACHCASE 解决这个问题.例如:

For now, you can work around this using FOREACH and CASE. For example:

START a=node(1), b=node(2), c=node(3)
OPTIONAL MATCH (a)-[r1:RELATIONSHIP]-(optional1)
OPTIONAL MATCH (b)-[r2:RELATIONSHIP]-(optional2)
FOREACH (o IN CASE WHEN optional1 IS NOT NULL THEN [optional1] ELSE [] END |
  CREATE (c)-[:NEW_RELATIONSHIP]->(optional1)
)
FOREACH (o IN CASE WHEN optional2 IS NOT NULL THEN [optional2] ELSE [] END |
  CREATE (c)-[:NEW_RELATIONSHIP]->(optional2)
)
DELETE r1, r2
RETURN a, b, c

我怀疑这会在未来的 Cypher 语言更新中变得更简单.

I suspect this will be made simpler in future Cypher language updates.

附注.您可能不应该再使用 START,但我想您只是为了示例方便.

PS. You probably shouldn't use START anymore, but I imagine you have simply for the convenience of an example.

这篇关于在没有找到匹配的可选匹配之后创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆