简体   繁体   中英

Create relationship between nodes in neo4j

In Neo4j, I want to create relationship between nodes where both the nodes are related with a common node.

For Example, I have three nodes where 2 student nodes and a teacher node. I already have relation between student and teacher. Now I want to create a third relationship between student to student where both the students are already related to the same teacher. 在此处输入图片说明

In the above image T1 and T2 are teacher nodes. S1, S2, S3 and S4 are student nodes. The relationships which are in blue is already created. now I want to create the relationships which are given in yellow.

Please help. Thanks in Advance.

1) If simple:

// Choose a pair of students with common teacher
MATCH p = (S1:Student)-[:has_teacher]->(T:Teacher)<-[:has_teacher]-(S2:Student)
    // Without [:common_teacher] relationships
    WHERE NOT (S1)-[:common_teacher]-(S2)
// And create relationships
CREATE (S1)-[r1:common_teacher]->(S2)
CREATE (S2)-[r2:common_teacher]->(S1)

2) If on the other:

You do not need to create additional relationships between the students, because they are already bound by a common teacher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM