Cypher语句描述了如何使用Cypher语法操作图中的节点、关系和路径等元素,以下列举了一些常用的操作语句。
1. 查询操作Match语句
1.1 查询所有节点
MATCH(n) RETURN n
1.2 查询所有带标签的节点
MATCH(carSeries:CarSeries)
RETURN carSeries.name
返回内容
carSeries.name
"宝马1系"
"宝马2系"
"宝马3系"
1.3 查询所有的节点并返回关联的节点
查询宝马5系下面有哪些车型
MATCH (carSeries {name: '宝马5系'})-[]-(carTye)
RETURN carTye.carTypeName
返回结果
carTye.carTypeName
"2020款525L钻石款"
"2020款525L豪华款"
"2020款525L运动款"
1.4 查询节点关联的标签
MATCH (:CarSeries {name: '宝马5系'})-[]-(carTye:CarType)
RETURN carTye.carTypeName
1.5 查询节点带指向关系
MATCH (:CarSeries {name: '宝马5系'})-[]->(carTye)
RETURN carTye.carTypeName
1.6 查询返回关系
MATCH (:CarSeries {name: '宝马5系'})-[r]->(carTye)
RETURN type(r)
返回内容
type(r)
"include"
"include"
"include"
1.7 根据关系类型查询
查询供应M34型号底盘的供应商
MATCH (:CarChassis {chassisName: 'M34'})<-[:supplier]-(supplier)
RETURN supplier.supplierName
supplier.supplierName
"摩比斯"
"摩比斯"
1.8 查询多层关系
MATCH p = (carBrand{brandName:'宝马'}) - [:include*2] - (carType)
RETURN relationships(p)
返回结果
elationships(p)
[
{
"identity": 8,
"start": 16,
"end": 20,
"type": "include",
"properties": {
}
}
,
{
"identity": 17,
"start": 20,
"end": 29,
"type": "include",
"properties": {
}
}
]
[
{
"identity": 8,
"start": 16,
"end": 20,
"type": "include",
"properties": {
}
}
,
{
"identity": 18,
"start": 20,
"end": 30,
"type": "include",
"properties": {
}
}
]
[
{
"identity": 8,
"start": 16,
"end": 20,
"type": "include",
"properties": {
}
}
,
{
"identity": 19,
"start": 20,
"end": 31,
"type": "include",
"properties": {
}
}
]
[
{
"identity": 8,
"start": 16,
"end": 20,
"type": "include",
"properties": {
}
}
,
{
"identity": 20,
"start": 20,
"end": 32,
"type": "include",
"properties": {
}
}
]
[
{
"identity": 10,
"start": 16,
"end": 22,
"type": "include",
"properties": {
}
}
,
{
"identity": 21,
"start": 22,
"end": 33,
"type": "include",
"properties": {
}
}
]
[
{
"identity": 10,
"start": 16,
"end": 22,
"type": "include",
"properties": {
}
}
,
{
"identity": 22,
"start": 22,
"end": 34,
"type": "include",
"properties": {
}
}
]
[
{
"identity": 10,
"start": 16,
"end": 22,
"type": "include",
"properties": {
}
}
,
{
"identity": 23,
"start": 22,
"end": 35,
"type": "include",
"properties": {
}
}
]
1.9 根据节点Id或关系ID查询
MATCH(n)
WHERE id(n)=22
RETURN n
根据关系ID查询
MATCH()-[r]->()
where id(r)=8
RETURN r
返回关系结果
r
{
"identity": 8,
"start": 16,
"end": 20,
"type": "include",
"properties": {
}
}
2. 可选查询OPTIONAL MATCH语句
OPTIONAL MATCH与MATCH语句类型,不同的是,如果查询没有匹配到结果的话就会返回 null 。在这点上,OPTIONAL MATCH有些像SQL中的 out join 语句。
OPTIONAL MATCH的表现方式与WHERE语句类似,具体体现效果请看下面内容。
2.1 查询宝马 2020款320L运动款 下面关联的节点
MATCH(a:CarType{carTypeName:'2020款320L运动款'})
OPTIONAL MATCH(a)-[]->(x)
RETURN x
3. 返回RETURN语句
3.1 返回所有的对象
MATCH p=(a {brandName:'宝马'}) -[r] - (b)
RETURN *
返回结果
3.2 返回多个表达式
MATCH (a {brandName:'宝马'})
RETURN a.brandName, "牛逼Class", (a)-[]-()
返回结果
a.brandName
"牛逼Class"
(a)-[]-()
"宝马"
"牛逼Class"
[
{ "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "end": { "identity": 28, "labels": [ "CarSeries" ], "properties": { "name": "宝马Z系", "description": "宝马入门级跑车", "status": "INUSE" } }, "segments": [ { "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "relationship": { "identity": 16, "start": 16, "end": 28, "type": "include", "properties": {
}
},
"end": {
"identity": 28, "labels": [ "CarSeries" ], "properties": { "name": "宝马Z系", "description": "宝马入门级跑车", "status": "INUSE" } } } ], "length": 1.0 }
,
{ "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "end": { "identity": 27, "labels": [ "CarSeries" ], "properties": { "name": "宝马X系", "description": "宝马SUV车", "status": "INUSE" } }, "segments": [ { "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "relationship": { "identity": 15, "start": 16, "end": 27, "type": "include", "properties": {
}
},
"end": {
"identity": 27, "labels": [ "CarSeries" ], "properties": { "name": "宝马X系", "description": "宝马SUV车", "status": "INUSE" } } } ], "length": 1.0 }
]
4. WITH 语句
with从句可以连接多个查询的结果,将上一个查询的结果用作下一个查询的开始。
WITH语句的两种常用方法:
- 通过使用oder by 和limit,with可以限制传入下一个match子查询语句的实体数目。
- 对聚合值过滤。
4.1 对聚合结果进行过滤
MATCH(carBrand{brandName: '宝马'})-[]-(unkonwnNode)-[]->()
WITH unkonwnNode, count(*) as cn
WHERE cn>1
RETURN unkonwnNode.id
4.2 Order 排序
MATCH(n) WITH n
ORDER BY n.id DESC
LIMIT 3
RETURN n
4.3 路径搜索限制分支数
MATCH(n{name:'宝马3系'})-[]-(m)
WITH m
ORDER BY m.name DESC
LIMIT 1
MATCH (m)-[]-(o)
RETURN o.name
5. WHERE语句
MATCH(n)
WHERE n.name='Peter' XOR (n.age<30 AND n.name='Fucky') OR NOT (n.name='Peter')
RETURN n.name, n.age
6. ORDER BY语句
MATCH(n)
RETRUN n.length, n.name
ORDER BY n.length
7. SKIP和LIMIT语句
MATCH(n)
RETURN n.name
ORDER BY n.name
SKIP 3
LIMIT 2
8. DELETE 语句
DELETE语句用来删除图中的节点、关系或路径
MATCH(n:Useless)
DELETE n
9. REMOVE 语句
REMOVE语句用来移除节点的属性或者标签
MATCH(address {name: 'zhangsan'})
REMOVE address.age
RETURN address