同时查询 MATCH 和 CREATE

query with both MATCH and CREATE

我有 3 个节点:

当用户保存托盘时,我想:

之后,我想删除此 :Palette 与数据库中 :Color 节点的所有 :INCLUDES 关系(以便之后创建新节点)。

这是我的查询(我用硬编码字符串替换了变量,因此更容易执行):

MATCH (u:User)
    WHERE u.id = '4f3d1904'
MERGE (p:Palette {name: 'Test'})
    ON CREATE 
        SET p.name = "Test"
        MERGE (u)-[cr:CREATED]->(p)
MERGE (u)-[sa:SAVED]->(p)
MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()
DELETE in

当运行查询时出现以下错误:

WITH is required between MERGE and MATCH (line 8, column 1 (offset: 181)) "MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()" ^

但是如果我添加 WITH 我会得到以下结果:

MATCH (u:User)
    WHERE u.id = '4f3d1904'
MERGE (p:Palette {name: 'Test'})
    ON CREATE 
        SET p.name = "Test"
        MERGE (u)-[cr:CREATED]->(p)
MERGE (u)-[sa:SAVED]->(p)
WITH
MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()
DELETE in

Invalid input ')': expected whitespace or a relationship pattern (line 9, column 32 (offset: 217)) "MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()" ^

我做错了什么?

MERGE 和 MATCH 阶段(或 MATCH 和 MATCH)之间需要一个 WITH,以便在后者中使用前者的结果。

在您的情况下,您可以像这样使用已有的 p

...
WITH p 
MATCH (p)-[in:INCLUDES]->() 
DELETE in

这样您就不需要再找了。没有 WITH,就像两个不同的查询。