Multiple independent "WITHs"?


(tlmn) #1

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?


(Michael Hunger) #2

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

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.


(tlmn) #3

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