Cypher查询语言
最后更新于:2022-04-02 04:01:24
[TOC]
> [参考](https://zhuanlan.zhihu.com/p/88745411)
## 语法
## 删除数据库
```
MATCH (n) DETACH DELETE n
```
MATCH是匹配操作,而小括号()代表一个节点node(可理解为括号类似一个圆形),括号里面的n为标识符
## 创建节点
**创建多人物个节点**
```
CREATE (n:Person {name:'John'}) RETURN n
CREATE (n:Person {name:'Sally'}) RETURN n
CREATE (n:Person {name:'Steve'}) RETURN n
CREATE (n:Person {name:'Mike'}) RETURN n
CREATE (n:Person {name:'Liz'}) RETURN n
CREATE (n:Person {name:'Shawn'}) RETURN n
```
`CREATE`是**创建**操作,`Person`是**标签**,代表节点的类型。花括号{}代表节点的**属性**,属性类似Python的字典
**创建地区节点**
```
CREATE (n:Person {name:'Sally'}) RETURN n
CREATE (n:Person {name:'Steve'}) RETURN n
CREATE (n:Person {name:'Mike'}) RETURN n
CREATE (n:Person {name:'Liz'}) RETURN n
CREATE (n:Person {name:'Shawn'}) RETURN n
```
## 创建关系
```
MATCH (a:Person {name:'Shawn'}),
(b:Person {name:'Sally'})
MERGE (a)-[:FRIENDS {since:2001}]->(b)
// 创建更多
MATCH (a:Person {name:'Shawn'}), (b:Person {name:'John'}) MERGE (a)-[:FRIENDS {since:2012}]->(b)
MATCH (a:Person {name:'Mike'}), (b:Person {name:'Shawn'}) MERGE (a)-[:FRIENDS {since:2006}]->(b)
MATCH (a:Person {name:'Sally'}), (b:Person {name:'Steve'}) MERGE (a)-[:FRIENDS {since:2006}]->(b)
MATCH (a:Person {name:'Liz'}), (b:Person {name:'John'}) MERGE (a)-[:MARRIED {since:1998}]->(b)
```
创建 a->b 两个人的边,原因为 FRIENDS
创建人与地区的边
```
MATCH (a:Person {name:'John'}), (b:Location {city:'Boston'}) MERGE (a)-[:BORN_IN {year:1978}]->(b)
// 创建更多
MATCH (a:Person {name:'Liz'}), (b:Location {city:'Boston'}) MERGE (a)-[:BORN_IN {year:1981}]->(b)
MATCH (a:Person {name:'Mike'}), (b:Location {city:'San Francisco'}) MERGE (a)-[:BORN_IN {year:1960}]->(b)
MATCH (a:Person {name:'Shawn'}), (b:Location {city:'Miami'}) MERGE (a)-[:BORN_IN {year:1960}]->(b)
MATCH (a:Person {name:'Steve'}), (b:Location {city:'Lynn'}) MERGE (a)-[:BORN_IN {year:1970}]->(b)
```
创建后效果
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/67/f3/67f3ffae1d42106809b4a165e29578e8_483x328.png)
## 查询
查询下所有在Boston出生的人物
```
MATCH (a:Person)-[:BORN_IN]->(b:Location {city:'Boston'}) RETURN a,b
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/0d/b0/0db0956a4db4839c319d279929d38f17_203x189.png)
查询所有对外有关系的节点
```
MATCH (a)-->() RETURN a
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/98/59/9859325fb67130a238d783b690531ff3_421x217.png)
查询所有有关系的节点
```
MATCH (a)--() RETURN a
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/25/7b/257b1f0838e400281288dedeee1d8658_537x344.png)
查询所有对外有关系的节点,以及关系类型
```
MATCH (a)-[r]->() RETURN a.name, type(r)
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/43/dd/43dd7d00c2b92d68e194fac1064e092b_346x339.png)
查询所有有结婚关系的节点
```
MATCH (n)-[:MARRIED]-() RETURN n
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/ce/03/ce037e89814be5d68dcf07ea2eb4897a_110x185.png)
查找某人的朋友的朋友
```
MATCH (a:Person {name:'Mike'})-[r1:FRIENDS]-()-[r2:FRIENDS]-(friend_of_a_friend) RETURN friend_of_a_friend.name AS fofName
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/eb/20/eb20a10c8a0643ab57507af349bcee12_95x94.png)
## 创建节点并建好关系
```
CREATE (a:Person {name:'Todd'})-[r:FRIENDS]->(b:Person {name:'Carlos'})
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/39/37/39376c73ca92f8b77933c453c91a9e31_168x165.png)
## 增加/修改节点的属性
```
MATCH (a:Person {name:'Liz'}) SET a.age=34
MATCH (a:Person {name:'Shawn'}) SET a.age=32
MATCH (a:Person {name:'John'}) SET a.age=44
MATCH (a:Person {name:'Mike'}) SET a.age=25
```
## 删除节点的属性
```
MATCH (a:Person {name:'Mike'}) SET a.test='test'
MATCH (a:Person {name:'Mike'}) REMOVE a.test
```
## 删除节点
```
MATCH (a:Location {city:'Portland'}) DELETE a
```
## 删除有关系的节点
```
MATCH (a:Person {name:'Todd'})-[rel]-(b:Person) DELETE a,b,rel
```
';