在 cypher 中使用 'if' 语法执行不同的查询
Using 'if' syntax in cypher to execute diferrent queries
有没有办法为下一个任务编写一个查询。
I have (a:A)-[r:RELATES]->(b:B)
and if r.value = 1 with a
Match (c:C{id:a.id}) with c, a
Merge (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 1
and if r.value = 0
// I don't create relationship between c and a but if it exists i set q.value = 0
Match (c)-[q:RELATED_TO]->(a)
Set q.value = 0
我尝试为此任务使用 FOREACH 查询
Match (a:A)-[r:RELATES]->(b:B) with a, r, b
Match (c:C{id:a.id}) with a, r, b, c
FOREACH(ignoreMe IN CASE WHEN r.value = 1 THEN [1] ELSE [] END |
Merge (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 1
)
FOREACH(ignoreMe IN CASE WHEN NOT r.value = 1 THEN [1] ELSE [] END |
Match (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 0
)
但是我在 FOREACH 中收到 MATCH 的无效使用。似乎可以用两个查询来完成,但我想知道是否可以用一个
这在逻辑上似乎等同于您要执行的操作:
MATCH (a:A)-[r:RELATES]->(b:B), (c:C {id:a.id})
OPTIONAL MATCH (c)-[q:RELATED_TO]->(a)
WHERE r.value = 0
SET q.value = 0
WITH c, a, r
WHERE r.value = 1
MERGE (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 1;
- 如果
r.value
是0
并且q
关系已经存在,则将q.value
设置为0
。
- 如果
r.value
是1
,则在必要时创建q
关系;并且,如果 q
之前不存在,则将 q.value
设置为 1
.
有没有办法为下一个任务编写一个查询。
I have (a:A)-[r:RELATES]->(b:B)
and if r.value = 1 with a
Match (c:C{id:a.id}) with c, a
Merge (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 1
and if r.value = 0
// I don't create relationship between c and a but if it exists i set q.value = 0
Match (c)-[q:RELATED_TO]->(a)
Set q.value = 0
我尝试为此任务使用 FOREACH 查询
Match (a:A)-[r:RELATES]->(b:B) with a, r, b
Match (c:C{id:a.id}) with a, r, b, c
FOREACH(ignoreMe IN CASE WHEN r.value = 1 THEN [1] ELSE [] END |
Merge (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 1
)
FOREACH(ignoreMe IN CASE WHEN NOT r.value = 1 THEN [1] ELSE [] END |
Match (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 0
)
但是我在 FOREACH 中收到 MATCH 的无效使用。似乎可以用两个查询来完成,但我想知道是否可以用一个
这在逻辑上似乎等同于您要执行的操作:
MATCH (a:A)-[r:RELATES]->(b:B), (c:C {id:a.id})
OPTIONAL MATCH (c)-[q:RELATED_TO]->(a)
WHERE r.value = 0
SET q.value = 0
WITH c, a, r
WHERE r.value = 1
MERGE (c)-[q:RELATED_TO]->(a)
ON CREATE SET q.value = 1;
- 如果
r.value
是0
并且q
关系已经存在,则将q.value
设置为0
。 - 如果
r.value
是1
,则在必要时创建q
关系;并且,如果q
之前不存在,则将q.value
设置为1
.