Overview
The LET
statement allows you to add columns to the current working table by defining new variables. Each variable is assigned a value using the =
operator.
<let statement> ::=
"LET" <let variable definition> [ { "," <let variable definition> }... ]
<let variable definition> ::= <binding variable> "=" <value expression>
Details
LET
does not change the number of records of the working table.LET
does not modify existing columns of the working table unless you redefine existing variables withinLET
.- You cannot define a new variable and reference it within the same
LET
. - If any existing variable is referenced in
<value expression>
s withinLET
, theLET
statement is effectively replaced by the statementCALL (IBVRL) { BVDB RETURN RIL }
, where,IBVRL
(Incoming Binding Variables Reference) is the comma-separated list of all variables referenced in<value expression>
s.BVDB
(Binding Variable Definition Block) is the space-separated concatenation of all<let variable definition>
s.RIL
(Return Item List) is the comma-separated list of all<binding variable>
s.
Example Graph
The following examples run against this graph:
To create this graph, run the following query against an empty graph:
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)
Defining Variables with Constants
LET s = 6, a = "Alex"
MATCH (p:Paper) WHERE p.score = s AND p.author = a
RETURN p.title, s, a
Result:
p.title | s | a |
---|---|---|
Efficient Graph Search | 6 | Alex |
Defining Variables with Existing Variables
MATCH (x:Paper)
LET recommended = x.score > 7
RETURN x.title, recommended
Result:
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
Result:
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 |