UNION ALL 可以将两个 RETURN 子句中的同名返回值进行首尾拼接。由于其不对拼接后的结果进行去重,其效率高于 UNION 子句。
语法:
... RETURN <expression1_A>
as <alias_A>
, <expression1_B>
as <alias_B>
, ...
UNION ALL
... RETURN <expression2_A>
as <alias_A>
, <expression2_B>
as <alias_B>
, ...
参数:
- <expression1>:第一个 UQL 语句的返回值
- <expression2>:第二个 UQL 语句的返回值,需要和第一个 UQL 语句的返回值数量一致,别名相同的返回值的数据结构必须一致
- <alias>:返回值的别名(顺序可打乱),同名的返回值将被拼接
例如,将两个 RETURN 子句中的异源返回值 a 和 b 分别拼接:
uncollect [1,2,3]) as a
uncollect [3,4,5]) as b
return a, b
union all
uncollect [1,2]) as a
uncollect [3,5]) as b
return a, b
示例图集:(以下示例将在本图基础上运行)
在空图集中依次运行以下各行代码以创建该图数据:create().node_schema("student").node_schema("course")
create().node_property(@*, "name").node_property(@student, "age", int32).node_property(@course, "credit", int32)
insert().into(@student).nodes([{_id:"S001", _uuid:1, name:"Jason", age:25}, {_id:"S002", _uuid:2, name:"Lina", age:23}, {_id:"S003", _uuid:3, name:"Eric", age:24}, {_id:"S004", _uuid:4, name:"Emma", age:26}, {_id:"S005", _uuid:5, name:"Pepe", age:24}])
insert().into(@course).nodes([{_id:"C001", _uuid:6, name:"French", credit:4}, {_id:"C002", _uuid:7, name:"Math", credit:5}])
insert().into(@default).edges([{_uuid:1, _from_uuid:1, _to_uuid:6}, {_uuid:2, _from_uuid:2, _to_uuid:6}, {_uuid:3, _from_uuid:3, _to_uuid:6}, {_uuid:4, _from_uuid:2, _to_uuid:7}, {_uuid:5, _from_uuid:3, _to_uuid:7}, {_uuid:6, _from_uuid:4, _to_uuid:7}, {_uuid:7, _from_uuid:5, _to_uuid:7}])
一般用法
示例:查找选择了法语课的 24 岁以下(含)的学生,和选择了数学课的 24 岁以上(含)的学生,返回这些学生,无需去重
n({@course.name == "French"}).e().n({@student.age <= 24} as n) return n.name
union all
n({@course.name == "Math"}).e().n({@student.age >= 24} as n) return n.name
Lina
Eric
Pepe
Eric
Emma