概述
LET
语句可通过定义新变量向当前工作表添加列。使用=
运算符为变量赋值。
<let statement> ::=
"LET" <let variable definition> [ { "," <let variable definition> }... ]
<let variable definition> ::= <binding variable> "=" <value expression>
详情
LET
不会改变工作表的记录数。LET
不会修改工作表已有列,除非在LET
语句内重新定义已有变量。- 无法在同一条
LET
语句中定义新变量并引用它。 - 如果在
LET
语句的<value expression>
部分引用了已有变量,则须将LET
语句替换成CALL (IBVRL) { BVDB RETURN RIL }
语句,其中:IBVRL
(写入绑定变量引用)是<value expression>
中引用的所有变量列表,由逗号分隔。BVDB
(绑定变量定义块)拼接了所有<let variable definition>
,由空格分隔。RIL
(返回项列表)是所有<binding variable>
列表,由逗号分隔。
示例图集
以下示例根据该图集运行:
在空图集中运行以下语句创建示例图集:
INSERT (p1:Paper {_id:"P1", title:'Efficient Graph Search', score:6, author:'Alex'}),
(p2:Paper {_id:"P2", title:'Optimizing Queries', score:9, author:'Alex'}),
(p3:Paper {_id:"P3", title:'Path Patterns', score:6, author:'Zack'}),
(p1)-[:Cites]->(p2),
(p2)-[:Cites]->(p3)
使用常量定义变量
LET s = 6, a = "Alex"
MATCH (p:Paper) WHERE p.score = s AND p.author = a
RETURN p.title, s, a
结果:
p.title | s | a |
---|---|---|
Efficient Graph Search | 6 | Alex |
使用已有变量定义变量
MATCH (x:Paper)
LET recommended = x.score > 7
RETURN x.title, recommended
结果:
x.title | recommended |
---|---|
Optimizing Queries | 1 |
Efficient Graph Search | 0 |
Path Patterns | 0 |
MATCH p = ()->{1,2}()
LET length = path_length(p)
RETURN p, length
结果:
p | length |
---|---|
(:Paper {_id: "P1", score: 6, title: "Efficient Graph Search", author: "Alex"})-[:Cites]->(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex"}) | 1 |
(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex"})-[:Cites]->(:Paper {_id: "P3", score: 6, title: "Path Patterns", author: "Zack"}) | 1 |
(:Paper {_id: "P1", score: 6, title: "Efficient Graph Search", author: "Alex"})-[:Cites]->(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex"})-[:Cites]->(:Paper {_id: "P3", score: 6, title: "Path Patterns", author: "Zack"}) | 2 |