CALL 可以对每个子查询的结果进行独立的操作和运算。该子句将这些操作和运算的语句包裹在大括号 {}
中,并通过 WITH 子句传入触发子查询的别名,通过 RETURN 子句将子查询结果运算后的别名传出。
语法:
CALL {
WITH <alias_In>
, <alias_In>
, ...
...
RETURN <expression>
as <alias_Out>
, <expression>
as <alias_Out>
, ...
}
参数:
- <alias_In>:触发子查询的别名
- <expression>:子查询的返回值
- <alias_Out>:子查询的返回值别名,<expression> 为别名时 <alias_Out> 可省略
例如,将每个子查询结果 p 的第一行跳过:
find().nodes([1, 5]) as nodes
call {
with nodes
n(nodes).e()[:2].n() as p
skip 1
return p as path
}
return path
示例图集:(以下示例将在本图基础上运行)
在空图集中依次运行以下各行代码以创建该图数据:create().node_schema("country").node_schema("movie").node_schema("director").edge_schema("filmedIn").edge_schema("direct")
create().node_property(@*, "name")
insert().into(@country).nodes([{_id:"C001", _uuid:1, name:"France"}, {_id:"C002", _uuid:2, name:"USA"}])
insert().into(@movie).nodes([{_id:"M001", _uuid:3, name:"Léon"}, {_id:"M002", _uuid:4, name:"The Terminator"}, {_id:"M003", _uuid:5, name:"Avatar"}])
insert().into(@director).nodes([{_id:"D001", _uuid:6, name:"Luc Besson"}, {_id:"D002", _uuid:7, name:"James Cameron"}])
insert().into(@filmedIn).edges([{_uuid:1, _from_uuid:3, _to_uuid:1}, {_uuid:2, _from_uuid:4, _to_uuid:1}, {_uuid:3, _from_uuid:3, _to_uuid:2}, {_uuid:4, _from_uuid:4, _to_uuid:2}, {_uuid:5, _from_uuid:5, _to_uuid:2}])
insert().into(@direct).edges([{_uuid:6, _from_uuid:6, _to_uuid:3}, {_uuid:7, _from_uuid:7, _to_uuid:4}, {_uuid:8, _from_uuid:7, _to_uuid:5}])
一般用法
示例:不使用 GROUP BY 子句,查询在每个国家各拍摄了几部电影
find().nodes({@country}) as nodes
call {
with nodes
n(nodes).e().n({@movie} as n)
return count(n) as number
}
return table(nodes.name, number)
| nodes.name | number |
|------------|--------|
| France | 2 |
| USA | 3 |