条件表达式COALESCE
返回一组给定表达式的首个非空值。在遇见空值时,使用表达式COALESCE
可有效替换默认值,特别是在计算过程或结果展示时,可用来避免空值。
<coalesce expression> ::=
"COALESCE" "(" <value expression> { "," <value expression> }... ")"
表达式COALESCE(V1, V2, V3)
等同于以下CASE
表达式:
CASE
WHEN NOT V1 IS NULL THEN V1
WHEN NOT V2 IS NULL THEN V2
ELSE V3
END
示例图集
以下示例根据该图集运行:
在空图集中运行以下语句创建示例图集:
INSERT (p1:Paper {_id:'P1', title:'Efficient Graph Search', score:6, author:'Alex', publisher:'PulsePress'}),
(p2:Paper {_id:'P2', title:'Optimizing Queries', score:9, author:'Alex'}),
(p3:Paper {_id:'P3', title:'Path Patterns', score:7, author:'Zack', publisher:'BrightLeaf'}),
(p1)-[:Cites {weight:2}]->(p2),
(p2)-[:Cites {weight:1}]->(p3)
示例
MATCH (n:Paper)
RETURN n.title, COALESCE(n.publisher, "N/A") AS publisher
结果:
n.title | publisher |
---|---|
Efficient Graph Search | PulsePress |
Optimizing Queries | N/A |
Path Patterns | BrightLeaf |