2017-04-03 41 views
1

返回考虑follwing CQL查询UNWIND不Neo4j的

MATCH (n:Label1) WITH n 
OPTIONAL MATCH (n)-[r:REL_1]-(:Label2 {id: 5}) 
WHERE r is NULL OR r.d < 12345 OR (r.d = 12345 OR r.c < 2) 
WITH n,r LIMIT 100 
WITH COLLECT({n: n, r: r}) AS rows 
MERGE (c:Label2 {id: 5}) 
WITH c, 
[b IN rows WHERE b.r.d IS NULL OR b.r.d < 12345] AS null_less_rows, 
[c IN rows WHERE (c.r.d = 12345 AND c.r.c < 2)] AS other_rows 
WITH null_less_rows, other_rows, c, null_less_rows+other_rows AS rows, size(null_less_rows+other_rows) AS count 
UNWIND null_less_rows AS null_less_row 
MERGE(s:Label1 {id: null_less_row.n.id}) 
MERGE(s)-[:REL_1 {d: 12345, c: 1}]->(c) 
WITH DISTINCT other_rows, c, rows, count 
UNWIND other_rows AS other_row 
MATCH(s:Label1 {id: other_row.n.id})-[str:REL_1]->(c) SET str.c = str.c + 1 
WITH rows, count 
RETURN rows, count 

当我EXCUTE查询,它应该返回行(按查询)计数。但是不要返回行,请将其计为结果语句。

Set 200 properties, created 100 relationships, statement completed in 13 ms. 

查询结构是否存在任何问题或使用UNWIND子句时出现问题。

+0

尝试插入'RETURN other_rows,行,最后'MERGE'后count'(和评论毕竟)。结果会是什么?附:对于未来:如果您要求帮助理解复杂的查询 - 举例说明可以检查此查询的输入数据。 http://stackoverflow.com/help/mcve –

回答

0

加成Micheael饥饿应答

UNWIND (CASE other_rows WHEN [] then [{n:{id: -2}}] else other_rows end) AS other_row 

由于我对数组的值进行操作,而不是空,我需要添加额外的条件,使它不能抛出任何错误mesaages。

这apllies两种情况下(other_rows,null_less_rows)

0

如果other_rows为null或为空,UNWIND将不会产生任何行。

您可以用解决它:

UNWIND case coalesce(size(other_rows),0) when 0 then [null] else other_rows end 
    as other_row