CREATE after OPTIONAL MATCH where no match was found

前端 未结 1 1595
栀梦
栀梦 2021-01-05 06:42

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 MATC         


        
相关标签:
1条回答
  • 2021-01-05 07:05

    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
    

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

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

    0 讨论(0)
提交回复
热议问题