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.

Multiple independent "WITHs"?

I got something like that:

MATCH (a:A)
WITH a.id as `aId`, a.acRef as `acRef`
MATCH (b:B)
WITH b.id as bId, b.bcRef as `bcRef`
MATCH (c:C)
WHERE acRef=c.aRef and bcRef=c.bRef
RETURN aId, bId, c.id as `cId`

but ofcourse it doesn't work because I'm not passing aId and acRef from second subquery to main query. I can do that but then query would execute for really long time (no association between a and b nodes).
Is it possible to make Cypher query with independent subqueries?

2 REPLIES 2

Not really sure why you do relational-style joining in a graphdb

MATCH (a:A) with collect(a) as aNodes
MATCH (b:B) with collect(b) as bNodes
MATCH (c:C)
WHERE c.aRef IN [a IN aNodes | a.acRef] and c.bRef IN [b IN bNodes | b.bcRef]
RETURN c.id as `cId`, head(a IN aNodes WHERE c.aRef = a.acRef | a.id ])  as aId, 
head(b IN bNodes WHERE c.bRef = b.bcRef | a.id ])  as bId

or

MATCH (a:A) 
MATCH (c:C) WHERE a.acRef=c.aRef
MATCH (b:B) WHERE b.bcRef =c.bRef
RETURN a.id as aId, b.id as bId, c.id as cId

depending on the query you should have indexes on the acRef, bcRef and aRef, bRef fields.

Thank you for quick response. I know that I shouldn't join like that
I just wondered if I could make temporary sets of nodes to use on later part of the query (like WITH in postgresql).