LET statement¶
The LET statement declares variables that can be used in subsequent statements.
The LET statement defines variables by adding new columns to the output table of the current statement. Variables declared in the LET statement must be included in a RETURN statement to be usable in statements following the keyword NEXT that follows the RETURN statement. For more information, see Statement block.
Syntax¶
- let_statement
let_statement ::=
'LET' variable_name '=' value_expression (',' variable_name '=' value_expression)*
Parameters¶
variable_name¶
The name of the variable declared. The name can be a regular identifier or a delimited identifier enclosed with backticks (`). It must be unique. For more information about identifiers, see Rules for naming identifiers.
value_expression¶
A constant or value expression, the evaluated value of which is assigned to the variable declared. The value can be of any value type. For more information, see Value expressions.
Examples¶
The following examples are based on the example dataset.
Assign constant values to variables¶
'''
LET a = 1, b = 2, c = 3
RETURN a, b, c
'''
Return:
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
Define variables with expressions¶
'''
LET a = 1 + 2, b = 3 + 4
LET c = a + b
RETURN a, b, c
'''
Return:
+---+---+----+
| a | b | c |
+---+---+----+
| 3 | 7 | 10 |
+---+---+----+
Define variables with node properties¶
'''
USE movie
MATCH (v@Actor) WHERE v.id = 199
LET a = v.name, b = upper(a)
RETURN b
'''
Return:
+--------------+
| b |
+--------------+
| JULIE KAVNER |
+--------------+
Define a variable with a subquery¶
'''
LET a = VALUE { USE movie MATCH (v@Actor) WHERE v.id = 199 RETURN v.name LIMIT 1 }
RETURN a
'''
Return:
+--------------+
| a |
+--------------+
| Julie Kavner |
+--------------+
Use variables in subsequent statements¶
'''
USE movie
LET a = 199
RETURN a
NEXT
USE movie
MATCH (v WHERE v.id = a) RETURN v
'''
Return:
+--------------------------------------------------------------------------------------------+
| v |
+--------------------------------------------------------------------------------------------+
| (289113618996264961@Movie:Movie{id:199,name:"Star Trek: First Contact"}) |
| (288832144019554307@User:Person{id:199}) |
| (288269194066132994@Actor_renamed:Person{birthDate:1951-09-07,id:199,name:"Julie Kavner"}) |
+--------------------------------------------------------------------------------------------+