条件表达式CASE
可用来判断单个或多个条件,并根据这些条件返回不同的结果。
UQL支持CASE
表达式的两种形式:
示例图集

create().node_schema("Paper").edge_schema("Cites")
create().node_property(@Paper, "title").node_property(@Paper, "score", int32).node_property(@Paper, "author").node_property(@Paper, "publisher").edge_property(@Cites, "weight", int32)
insert().into(@Paper).nodes([{_id:'P1', title:'Efficient Graph Search', score:6, author:'Alex', publisher: "PulsePress"}, {_id:'P2', title:'Optimizing Queries', score:9, author:'Alex'}, {_id:'P3', title:'Path Patterns', score:7, author:'Zack', publisher: "BrightLeaf"}])
insert().into(@Cites).edges([{_from:"P1", _to: "P2", weight: 2}, {_from:"P2", _to: "P3", weight: 1}])
Simple CASE
Simple CASE
表达式用来判断单个值与多个条件是否匹配,并返回首个匹配的结果。
case <expr>
when <value_1> then <result_1>
when <value_2> then <result_2>
...
else <result_default>
end
详情
<expr>
是表达式,如别名引用、聚合函数等。- 执行流程:
<expr>
依次与每个when
语句指定的<value_N>
进行比较。<value_N>
与<expr>
匹配时,将返回对应的<result_N>
。- 若无匹配结果,则返回
else
语句指定的<result_default>
。若省略else
,则默认返回null
。
find().nodes({@Paper.score > 6}) as n
return case count(n) when 3 then "Y" else "N" end AS result
结果:
result |
---|
N |
Searched CASE
Searched CASE
表达式用来判断多个条件,并返回首个条件判断为真的结果。
case
when <condition_1> then <result_1>
when <condition_2> then <result_2>
...
else <result_default>
end
详情
<condition_N>
为布尔值表达式,计算结果为真或假。- 执行流程:
- 以此判断各
<condition_N>
。 - 若
<condition_N>
为真,则立刻返回<result_N>
。 - 若所有
<condition_N>
为假,则返回else
语句指定的<result_default>
。若省略else
,则默认返回null
。
- 以此判断各
find().nodes({@Paper}) as n
return n.title,
case
when n.publisher is null then "Publisher N/A"
when n.score < 7 then -1
else n.author
end as note
结果:
n.title | note |
---|---|
Optimizing Queries | Publisher N/A |
Efficient Graph Search | -1 |
Path Patterns | Zack |