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.

How to create a relation somehow between two labels and not check each node?

Hi guys, I have imported two different CSV files and added the labels USERS & ORDERS to them. Into each node, we have an email field(it can be any field name) I want to create relationships with them to fetch them when needed. I want to have something similar to JOIN in the SQL world or LOOKUP in the NoSQL world.

For now, I find only one way to do it. It is to scan all nodes attached to one label then try to find the same field in nodes attached to another label and finally create a relationship. I believe it is not the best option with neo4j. Could someone help me with this?

 

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven

Try this:

merge (a:Users {name:"ABC", email:"abc@xmail.com"})
merge (a1:Users {name:"ACC", email:"acc@ymail.com"})
merge (a2:Users {name:"ABD", email:"add@pmail.com"})


merge (a3:Orders {name:"ABC", email:"abc@xmail.com"})
merge (a4:Orders {name:"ABCE", email:"acc@ymail.com"})
merge (a5:Orders {name:"ABCF", email:"abcf@xmail.com"})
 
Run the Cypher query:
match(a:Users)
match (b:Orders) where b.email = a.email
with a, b
merge (a)-[:CONNECTED_BY_EMAIL]->(b)
return a, b
Result:
Screen Shot 2022-07-26 at 1.59.35 PM.png

View solution in original post

1 REPLY 1

ameyasoft
Graph Maven

Try this:

merge (a:Users {name:"ABC", email:"abc@xmail.com"})
merge (a1:Users {name:"ACC", email:"acc@ymail.com"})
merge (a2:Users {name:"ABD", email:"add@pmail.com"})


merge (a3:Orders {name:"ABC", email:"abc@xmail.com"})
merge (a4:Orders {name:"ABCE", email:"acc@ymail.com"})
merge (a5:Orders {name:"ABCF", email:"abcf@xmail.com"})
 
Run the Cypher query:
match(a:Users)
match (b:Orders) where b.email = a.email
with a, b
merge (a)-[:CONNECTED_BY_EMAIL]->(b)
return a, b
Result:
Screen Shot 2022-07-26 at 1.59.35 PM.png