merge_关系
create
(olive:Person{chau:"Bill White",name:"Olive Stone",bornin:"New York"}),
(charlie:Person{chau:"John Brown",name:"Charlie Sheen",bornin:"New York"}),
(michael:Person{chau:"John Brown",name:"Michael Douglas",bornin:"New Jersey"}),
(martin:Person{chau:"Bob Brown",name:"Martin Sheen",bornin:"Ohio"}),
(rob:Person{chau:"Ted Green",name:"Rob Reiner",bornin:"New York"}),
(wallstreet:Movie{name:"WallStreet",title:"Wall Street"}),
(theAmerican:Movie{name:"TheAmericanPresident",title:"The American President"}),
(charlie)-[:ACTED_IN]->(wallstreet),
(olive)-[:DIRECTED]->(wallstreet),
(michael)-[:ACTED_IN]->(wallstreet),
(martin)-[:ACTED_IN]->(wallstreet),
(michael)-[:ACTED_IN]->(theAmerican),
(martin)-[:ACTED_IN]->(theAmerican),
(rob)-[:DIRECTED]->(theAmerican),
(charlie)-[:Father]->(martin)
(一)merge关系
1.关系已经存在,不返回结果
match(charlie:Person{name:"Charlie Sheen"}),(wallstreet:Movie{title:"Wall Street"})merge(charlie)-[r:ACTED_IN]->(wallstreet)
//(no changes, no records)
2.关系不存在,创建关系
match(olive:Person{name:"Olive Stone"}),(reiner:Person{name:"Rob Reiner"})merge(olive)-[:DIRECTED]->(movie:Movie)<-[:ACTED_IN]-(reiner)
//Added 1 label, created 1 node, created 2 relationships, statement completed in 3 ms.
3.创建无方向关系,方向任意
match(charlie:Person{name:"Charlie Sheen"}),(olive:Person{name:"Olive Stone"})merge(charlie)-[r:KNOWS]-(olive)return charlie,r,olive
4.利用原有属性创建新节点,并建立关系
//先创建节点,再创建关系
match(person:Person) merge(city:City{name:person.bornin}) merge(person)-[r:BORN_IN]->(city) return person.name,person.bornin,city
//同时创建节点和关系
match(person:Person) merge(person)-[r:BELONG]->(province:Province{name:person.bornin}) return person.name,person.bornin,province
posted on 2019-01-14 14:50 happygril3 阅读(152) 评论(0) 编辑 收藏 举报