概述
NEXT
语句串联多个语句,组成高级线性语句。
包含NEXT
语句的查询仅返回由最后一个RETURN
语句生成的最终表。
<next statement> ::= "NEXT" [ <yield clause> ] <statement>
<statement> ::= <linear data-accessing statement> | <composite query statement>
<linear data-accessing statement> ::=
<simple linear data-accessing statement> <result statement>
<simple linear data-accessing statement> ::= <simple data-accessing statement>...
<simple data-accessing statement> ::=
<insert statement>
| <set statement>
| <remove statement>
| <delete statement>
| <match statement>
| <filter statement>
| <let statement>
| <for statement>
| <order by statement>
| <limit statement>
| <skip statement>
| <call statement>
<result statement> ::=
<return statement> [ <order by statement> ] [ <skip statement> ] [ <limit statement> ]
详情
NEXT
语句之前必须有一个RETURN
语句,以便将待引用的变量传递给NEXT
后面的语句。YIELD
从句可用来选择和重命名变量,以供后续查询使用。
示例图集
以下示例根据该图集运行:
在空图集中运行以下语句创建示例图集:
INSERT (rowlock:User {_id: 'U01', name: 'rowlock'}),
(brainy:User {_id: 'U02', name: 'Brainy'}),
(purplechalk:User {_id: 'U03', name: 'purplechalk'}),
(mochaeach:User {_id: 'U04', name: 'mochaeach'}),
(lionbower:User {_id: 'U05', name: 'lionbower'}),
(c01:Club {_id: 'C01'}),
(c02:Club {_id: 'C02'}),
(rowlock)-[:Follows]->(brainy),
(mochaeach)-[:Follows]->(brainy),
(purplechalk)-[:Follows]->(mochaeach),
(purplechalk)-[:Follows]->(lionbower),
(brainy)-[:Joins {memberNo: 1}]->(c01),
(lionbower)-[:Joins {memberNo: 2}]->(c01),
(mochaeach)-[:Joins {memberNo: 9}]->(c02)
基本用法
本条查询返回的用户被U03
关注,同时是C01
的成员:
MATCH ({_id: "C01"})<-[:Joins]-(u1:User)
RETURN u1
NEXT
MATCH ({_id: "U03"})-[:Follows]->(u2:User)
WHERE u2 = u1
RETURN u2
结果:u2
_id | _uuid | schema | values |
---|---|---|---|
U05 | Sys-gen | User | {name: "lionbower"} |
使用分组结果
本条查询返回会员数最多的俱乐部中各用户的名字:
MATCH (c:Club)<-[:Joins]-()
RETURN c, count(c) AS cnt GROUP BY c
ORDER BY cnt DESC LIMIT 1
NEXT
MATCH (c)<-[:Joins]-(u)
RETURN collect_list(u.name)
结果:
collect_list(u.name) |
---|
["Brainy","lionbower"] |
使用Using Aggregated Result
本条查询插入从U01
指向C01
的新边Joins
,并将memberNo
属性设为下一个最大值:
MATCH ({_id: "C01"})<-[e1:Joins]-()
RETURN max(e1.memberNo) AS maxNo
NEXT
MATCH (u {_id: "U01"}), (c {_id: "C01"})
INSERT (c)<-[e2:Joins {memberNo: maxNo + 1}]-(u)
RETURN e2
结果:e2
_uuid |
_from |
_to |
_from_uuid |
_to_uuid |
schema |
values |
---|---|---|---|---|---|---|
Sys-gen | U01 | C01 | UUID of U01 | UUID of C01 | Joins | {memberNo: 3} |
NEXT YIELD
本条查询用户purplechalk
关注的用户所在的社团:
LET name = "purplechalk"
MATCH (:User {name: name})-[:Follows]->(u:User)
RETURN *
NEXT YIELD u
MATCH (u)-[:Joins]->(c:Club)
RETURN collect_list(c._id)
结果:
collect_list(c) |
---|
["C01","C02"] |