Neo4j系列导航:
neo4j及简单实践
cypher语法基础
cypher插入语法
cypher插入语法
cypher查询语法
cypher通用语法
cypher函数语法
neo4j索引及调优
3.通用语法
return、order by、limit、skip、with、unwind、union/union all、call和case
3.1.return语法
return用来返回数据, 一般放在语句的最后。它有个子句,分别为order by、limit和skip。
- 返回节点和标签
match (n {name:"zhangsan"}) return n, labels(n)
- 返回关系:
match (n:Person{name:"zhangsan"}-[r:Friennd]->(m))return n, r, m.name
- 返回关系节点和属性:
match (n:Person{name:"zhangsan"}-[r:Friennd]->(m))return p
- 返回路径:
match p=(n:Person)-[r:Friend]->(m:Person)return n.age
- 返回所有:
match (n:Person{name:"zhangsan"}-[r:Friennd]->(m))return *
- 返回并列别名:
match (n:Person{name:"zhangsan"}) return n.age as ownerName
- 表达式:
match (n:Person{name:"zhangsan"}) return n.age, "literal", (a)-->()
- 返回并去重:
match (n:Person{name:"zhangsan"})-->(m) return distinct m
3.2.order by语法(#3.2)
对输出结果进行排序操作,不能根据关系或者节点进行排序,只能根据属性排序!一般order by跟在return或者with后面。使用规则与传统关系型数据库基本相似。
- 根据属性排序:
match (n) return n order by n.name
- 多属性排序:
match (n) return n order by n.age, n.name
- 倒序排列节点:
match (n) return n order by n.name desc
- **null值排序:**当结果集中包含null值时,升序排列(null排在末尾),降序排序(null值排在最前面)
match (n) return n.title, n.name, n order by n.title
3.3.limit语法
limit语法用来限制输出结果的行数
- 输出前三个节点:
match (n) return n order by n.name limit 3
3.4.skip语法
跳过多少条数据(从哪行开始返回结果), 结合limit可实现传统关系型数据库分页的功能。
- 跳过前三条数据:
match (n) return n order by n.name skip 3
- 返回中间两个数据: 分页功能
match (n) return n order by n.name skip 1 limit 2
3.5.with语法
with语法在cypher中的作用是定义查询的作用域,控制查询结果的选择、过滤、转换和分割。它是进行复杂查询和数据处理的重要工具。
- with语句将分段的查询部分链接在一起,将查询结果从一部分以管道形式传递给另外一部分作为开始点。
- 使用with可以在将结果传递到后续查询之前对结果进行操作,操作可以是改变结果的形式或者数量,常见的一个用法就是限制传递给其他match语句的结果数。
- 创建列表:
with ["mouse", "chair", "door", "house"] as wordlist return wordlist
- 过滤聚合函数的结果:
match (n {name: "zhangsan" })--(m)-->(s) with m, count(*) as m_count where m_count>1 return m
- collect前排序:
match (n) with n order by n.name desc limit 3 return collect(n.name)
- 限制路径搜索的分支:
match (n {name: "zhangsan"})--(m) with m order by m.name desc limit 1 match (m)--(o) return o.name
3.5.unwind语法
cypher提供的一种列表遍历工具,结合case等语法可以写出许多复杂的查询,尤其是对于路径查询的处理。
- 将集合展开:
with ["mouse", "chair", "door", "house"] as wordlist unwind wordlist as x return x
- 将集合去重:
with [1,1,2,3,1] as coll unwind coll x with distinct x return collect(x) as set
3.6.union和union all语法
union和union all都是将多个查询结果组合起来。不同的是union会移除重复的行,union all会包含所有的结果不会移除重复的行。
注意: 使用union和union all都要保证查询到的列的名称和列的数量要完全一致。
- 去重:union
match (n:Person) return n.name as name union match(n:Movie) return b.title as name
- 不去重:union
match (n:Person) return n.name as name union all match(n:Movie) return b.title as name
3.7.call语法
调用数据库中的内置过程(Procedure),内置过程类似于关系型数据库中的存储过程,是一组完成特定功能的方法。但这一部分一般用到的不多。
- 调用数据库内置过程查询数据库中所有的点类型:
call db.labels()
- 调用内置过程并将结果绑定变量或过滤:
调用内置过程并将结果绑定变量
call db.labels() yield label return count(type) as numTypes
调用内置过程并过滤结果
call db.labels() yield label where starts with "P" return count(label) as numTypes
3.8.case语句(表达式)
case表达式根据条件对查询的结果进行处理
CASE
WHEN predicate THEN result
[WHEN ...]
[ELSE default]
END
示例:
match (n)
return
case n.eyes
when'blue'
when 1
when'brown'
when 2
else 3
end
as result