跳转至

UNWIND

UNWIND语句可以将列表拆分为单独的行,列表中的每个元素为一行。

UNWIND可以作为单独语句或语句中的子句使用。

语法

UNWIND <list> AS <alias> <RETURN clause>;

拆分列表

nebula> UNWIND [1,2,3] AS n RETURN n;
+---+
| n |
+---+
| 1 |
| 2 |
| 3 |
+---+

返回去重列表

UNWIND语句中使用WITH DISTINCT可以将列表中的重复项忽略,返回去重后的结果。

示例 1

  1. 拆分列表[1,1,2,2,3,3]
  2. 删除重复行。
  3. 排序行。
  4. 将行转换为列表。
nebula> WITH [1,1,2,2,3,3] AS n \
        UNWIND n AS r \
        WITH DISTINCT r AS r \
        ORDER BY r \
        RETURN collect(r);
+------------+
| collect(r) |
+------------+
| [1, 2, 3]  |
+------------+

示例 2

  1. 将匹配路径上的顶点输出到列表中。
  2. 拆分列表。
  3. 删除重复行。
  4. 将行转换为列表。
nebula> MATCH p=(v:player{name:"Tim Duncan"})--(v2) \
        WITH nodes(p) AS n \
        UNWIND n AS r \
        WITH DISTINCT r AS r \
        RETURN collect(r);
+----------------------------------------------------------------------------------------------------------------------+
| collect(r)                                                                                                           |
+----------------------------------------------------------------------------------------------------------------------+
| [("player100" :player{age: 42, name: "Tim Duncan"}), ("player101" :player{age: 36, name: "Tony Parker"}),            |
|("team204" :team{name: "Spurs"}), ("player102" :player{age: 33, name: "LaMarcus Aldridge"}),                          |
|("player125" :player{age: 41, name: "Manu Ginobili"}), ("player104" :player{age: 32, name: "Marco Belinelli"}),       |
|("player144" :player{age: 47, name: "Shaquile O'Neal"}), ("player105" :player{age: 31, name: "Danny Green"}),         |
|("player113" :player{age: 29, name: "Dejounte Murray"}), ("player107" :player{age: 32, name: "Aron Baynes"}),         |
|("player109" :player{age: 34, name: "Tiago Splitter"}), ("player108" :player{age: 36, name: "Boris Diaw"})]           |  
+----------------------------------------------------------------------------------------------------------------------+

最后更新: February 23, 2023