cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

When using WITH twice, variable cannot be found

Hello,

here is my code:

match (c:People)-[:LIKES]->(a:Object{objectName:'Skittles'})
WITH collect(c) as set1
match (d:People)-[: LIKES]->(a: Object{objectName:'Cheerios'})
WITH collect(d) as set2
RETURN apoc.coll.intersection(set1, set2) as set3

when I run this, it returns set1 is not found. But if I replace the last line with
RETURN apoc.coll.intersection(set2, set2) as set3
it works.

so, it seems that the first with is not passing set1 through.

Could anyone enlighten me with this issue?

ps:

my goal is to return people who both like Cheerios and Skittles. Any other way could get around with this issue could be fine.

Thanks in advance

2 REPLIES 2

Include set1 in your 2nd WITH statement. So,

WITH set1, collect(d) as set2

Each WITH clause is passing only the variables defined in it.

That is very helpful. Thank you. Appreciated.