我遇到了这个问题,我有一个使用WITH
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
RETURN connected
这很有效。创建KNOWS
关系后,connected
为true
其他false
。因此,此密码返回true
或false
。现在我想扩展这个
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
WITH y, connected
MATCH (x)-[zz:LIKES]->(y) <------------- this match
WITH zz, connected
RETURN zz, connected
现在connected
仅在标记的匹配成功时才返回true
或false
。否则它会给出一个空的结果。无论connected
是否成功,我都希望true
返回false
或MATCH
。
修改
抱歉,我在第二个查询中遗漏了一些内容。
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
WITH y, connected
MATCH (x)-[zz:LIKES]->(y) <------------- this match
MERGE x-[:LOVES]->(y) <------------- EDIT
WITH zz, connected
RETURN zz, connected
在解决匹配问题时可选匹配,如果出现null
错误,则会失败。有什么建议吗?
答案 0 :(得分:3)
我认为这样的事情可能会成功。
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
WITH y, connected
OPTIONAL MATCH (x)-[zz:LIKES]->(yy)
FOREACH(ignoreMe IN CASE WHEN yy IS null THEN [] ELSE [1] END |
MERGE x-[:LOVES]->(yy))
WITH zz, connected
RETURN zz, connected
OPTIONAL MATCH允许您保留所有初始行,然后您将为&#39; zz&#39;如果没有关系。