概述
RETURN
语句可以指定要包含在最终表中的列。每列由表达式定义,表达式可以包含变量、属性、函数和常量等。
<return statement> ::=
"RETURN" [ <set quantifier> ] { <"*"> | <return item list> }
[ <group by clause> ]
<return item list> ::= <return item> [ { "," <return item> }... ]
<return item> ::= <value expression> [ <return item alias> ]
<return item alias> ::= "AS" <identifier>
<set quantifier> ::= "DISTINCT" | "ALL"
详情
- 当指定集合量词
DISTINCT
后,每条返回项是分组操作的操作数,仅返回去重后的唯一记录。 *
返回当前工作表的所有列。- 使用关键词
AS
重命名列。 RETURN
语句支持使用GROUP BY
从句,通过指定分组键对最终表分组。
示例图集
以下示例根据该图集运行:
在空图集中运行以下语句创建示例图集:
INSERT (alex:Student {_id: 's1', name: 'Alex', gender: 'male'}),
(susan:Student {_id: 's2', name: 'Susan', gender: 'female'}),
(art:Course {_id: 'c1', name: 'Art', credit: 13}),
(literature:Course {_id: 'c2', name: 'Literature', credit: 15}),
(alex)-[:Take {year: 2024, term: 'Spring'}]->(art),
(susan)-[:Take {year: 2023, term: 'Fall'}]->(art),
(susan)-[:Take {year: 2023, term: 'Spring'}]->(literature)
返回点
本例中,与点绑定的变量返回各点标签及属性信息。
MATCH (n:Course)
RETURN n
结果: n
_id | _uuid | schema | values |
---|---|---|---|
c1 | Sys-gen | Course | {name: "Art", credit: 13} |
c2 | Sys-gen | Course | {name: "Literature", credit: 15} |
返回边
本例中,与边绑定的变量返回各边标签及属性信息。
MATCH ()-[e]->()
RETURN e
结果: e
_uuid |
_from |
_to |
_from_uuid |
_to_uuid |
schema |
values |
---|---|---|---|---|---|---|
Sys-gen | s2 | c1 | UUID of s2 | UUID of c1 | Take | {year: 2023, term: "Fall"} |
Sys-gen | s2 | c2 | UUID of s2 | UUID of c2 | Take | {year: 2023, term: "Spring"} |
Sys-gen | s1 | c1 | UUID of s1 | UUID of c1 | Take | {year: 2024, term: "Spring"} |
返回路径
本例中,绑定路径的变量返回每条路径中的点和边。每个元素都包含标签和属性信息。
MATCH p = ()-[:Take {term: "Spring"}]->()
RETURN p
结果:
p |
---|
(:Student {_id: "s1", gender: "male", name: "Alex"})-[:Take {year: 2024, term: "Spring"}]->(:Course {_id: "c1", name: "Art", credit: 13}) |
(:Student {_id: "s2", gender: "female", name: "Susan"})-[:Take {year: 2023, term: "Spring"}]->(:Course {_id: "c2", name: "Literature", credit: 15}) |
返回标签
labels()
函数可以返回点或边的标签。
MATCH ({_id: "s2"})-[e]->(n)
RETURN labels(e), labels(n)
结果:
labels(e) | labels(n) |
---|---|
Take | Course |
Take | Course |
返回属性
英文句号.
可以从绑定到点或边的变量中提取指定属性值。如果找不到点或边的指定属性,将返回null
值。
MATCH (:Student {name:"Susan"})-[]->(c:Course)
RETURN c.name, c.credit, c.type
结果:
c.name | c.credits | c.type |
---|---|---|
Literature | 15 | null |
Art | 13 | null |
返回所有数据
*
用来返回当前工作表中的所有列。
MATCH (s:Student {name:"Susan"})-[]->(c:Course)
RETURN *
结果:
s
_id | _uuid | schema | values |
---|---|---|---|
s2 | Sys-gen | Student | {name: "Susan", gender: "female"} |
s2 | Sys-gen | Student | {name: "Susan", gender: "female"} |
c
_id | _uuid | schema | values |
---|---|---|---|
c1 | Sys-gen | Course | {name: "Art", credit: 13} |
c2 | Sys-gen | Course | {name: "Literature", credit: 15} |
列别名
关键词AS
用来重命名返回的列。
MATCH (s:Student)-[t:Take]->(c:Course)
RETURN s.name AS Student, c.name AS Course, t.year AS TakenIn
结果:
Student | Course | TakenIn |
---|---|---|
Alex | Art | 2024 |
Susan | Art | 2023 |
Susan | Literature | 2023 |
返回去重记录
使用集合量词DISTINCT
返回去重记录。
MATCH ()-[e]->()
RETURN DISTINCT e.year
结果:
e.year |
---|
2023 |
2024 |
返回聚合数据
聚合函数,如sum()
和max()
,可在RETURN
语句中直接使用。
MATCH (:Student {name:"Susan"})-[]->(c:Course)
RETURN sum(c.credit)
结果:
sum(c.credit) |
---|
28 |
返回CASE数据
函数CASE
可在RETURN
语句中直接使用。
MATCH (n:Course)
RETURN n.name AS Course, CASE WHEN n.credit > 14 THEN "Y" ELSE "N" END AS Recommended
结果:
Course | Recommended |
---|---|
Art | N |
Literature | Y |
返回有限记录
LIMIT
语句可限制返回的记录数。
MATCH (n:Course)
RETURN n.name LIMIT 1
结果:
n.name |
---|
Art |
返回有序记录
ORDER BY
语句可根据特定值对结果排序。
MATCH (n:Course)
RETURN n ORDER BY n.credit DESC
结果: n
_id | _uuid | schema | values |
---|---|---|---|
c2 | Sys-gen | Course | {name: "Literature", credit: 15} |
c1 | Sys-gen | Course | {name: "Art", credit: 13} |
返回分组数据
可选择使用GROUP BY
从句对最终表分组。
MATCH ()-[e:Take]->()
RETURN e.term AS Term, count(e) GROUP BY Term
结果:
Term | e.year |
---|---|
Spring | 2 |
Fall | 1 |