概述
ORDER BY
语句可根据规则列表对当前工作表排序。
<order by statement> ::=
"ORDER BY" <sort specification> [ { "," <sort specification> }... ]
<sort specification> ::=
<value expression> [ <ordering specification> ] [ <null ordering> ]
<ordering specification> ::= "ASC" | "DESC"
<null ordering> ::= "NULLS FIRST" | "NULLS LAST"
详情
- 未提供排序规则时,默认使用
ASC
(升序)。若想反转顺序,可以显式使用关键词DESC
(降序)。 - 排序结果时,可使用
NULLS FIRST
和NULLS LAST
控制null
出现在非空值之前还是之后。若未明确空值排序时:- 当使用
ASC
排序规则时,默认使用NULLS LAST
。 - 当使用
DESC
排序规则时,默认使用NULLS FIRST
。
- 当使用
示例图集
以下示例根据该图集运行:
在空图集中运行以下语句创建示例图集:
INSERT (p1:Paper {_id:'P1', title:'Efficient Graph Search', score:6, author:'Alex', publisher:'PulsePress'}),
(p2:Paper {_id:'P2', title:'Optimizing Queries', score:9, author:'Alex'}),
(p3:Paper {_id:'P3', title:'Path Patterns', score:7, author:'Zack', publisher:'BrightLeaf'}),
(p1)-[:Cites {weight:2}]->(p2),
(p2)-[:Cites {weight:1}]->(p3)
根据属性排序
可根据特定属性值对表排序。
MATCH (n:Paper)
ORDER BY n.score
RETURN n.title, n.score
结果:
n.title | n.score |
---|---|
Efficient Graph Search | 6 |
Path Patterns | 7 |
Optimizing Queries | 9 |
根据元素变量排序
指定元素变量时,根据点或边的_uuid
对表排序。
MATCH (n:Paper)
RETURN n.title, element_id(n) ORDER BY n
结果:
n.title | element_id(n) |
---|---|
Optimizing Queries | 8718971077612535810 |
Efficient Graph Search | 8791028671650463745 |
Path Patterns | 12033620403357220867 |
根据表达式排序
可根据表达式结果对表排序。
MATCH p = (:Paper)->{1,2}(:Paper)
RETURN p, path_length(p) AS length ORDER BY length DESC
结果:
p | length |
---|---|
(:Paper {_id: "P1", score: 6, title: "Efficient Graph Search", author: "Alex", publisher: "PulsePress"})-[:Cites {weight: 2}]->(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex", publisher: null})-[:Cites {weight: 1]->(:Paper {_id: "P3", score: 7, title: "Path Patterns", author: "Zack", publisher: "BrightLeaf"}) | 2 |
(:Paper {_id: "P1", score: 6, title: "Efficient Graph Search", author: "Alex", publisher: "PulsePress"})-[:Cites {weight: 2]->(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex", publisher: null}) | 1 |
(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex", publisher: null})-[:Cites {weight: 1]->(:Paper {_id: "P3", score: 7, title: "Path Patterns", author: "Zack", publisher: "BrightLeaf"}) | 1 |
多级排序
存在多个排序规则时,表首先根据第一个排序规则排序;对于相等的值,根据下一个排序规则排序,以此类推。
MATCH (n:Paper)
RETURN n.title, n.author, n.score
ORDER BY n.author DESC, n.score
结果:
n.title | n.author | n.score |
---|---|---|
Path Patterns | Zack | 7 |
Efficient Graph Search | Alex | 6 |
Optimizing Queries | Alex | 9 |
排序后删除和保留记录
可以将SKIP
和LIMIT
语句与ORDER BY
语句结合使用,从表的开头跳过指定数量的记录,并限制保留的记录数。
本条查询返回得分第二和第三高的两篇论文的题目:
MATCH (n:Paper)
RETURN n.title, n.score
ORDER BY n.score DESC SKIP 1 LIMIT 2
结果:
n.title | n.score |
---|---|
Path Patterns | 7 |
Efficient Graph Search | 6 |
空值排序
排序结果时,可使用NULLS FIRST
和NULLS LAST
控制null
出现在非空值之前还是之后。
本条查询返回得分第二和第三高的两篇论文的题目:
MATCH (n:Paper)
RETURN n.title, n.publisher
ORDER BY n.publisher NULLS FIRST
结果:
n.title | n.score |
---|---|
Optimizing Queries | null |
Path Patterns | BrightLeaf |
Efficient Graph Search | PulsePress |