Home > Net >  Why does the `WITH` clause need to be reminded about the variable in Cypher?
Why does the `WITH` clause need to be reminded about the variable in Cypher?

Time:10-28

Say in group property I have values cat dog and chicken egg. I want to assign to property gid the initial characters of the values in group, i.e. cd and ce. This is my query:

match (n) 
with split(n.group," ") as array 
set n.gid=left(array[0],1) left(array[1],1) 
return n.name, n.gid;

I get this error:

Variable `n` not defined (line 2, column 48 (offset: 48))
"match (n) with split(n.group," ") as array set n.gid=left(array[0],1) left(array[1],1) return n.name, n.gid;"
                                                ^

However, this works:

match (n) 
with n, split(n.group," ") as array 
set n.gid=left(array[0],1) left(array[1],1) 
return n.name, n.gid;

I don't understand why is that? I check the WITH documentation but I don't see any problem?

CodePudding user response:

From the WITH docs itself

It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query.

In the first query,

match (n) 
with split(n.group," ") as array 
...

only array is carried forward to the rest of the query, n is forgotten., which is why you get the error.

  • Related