WITH

WITH子句可以获取并处理查询前半部分的结果,并将处理结果作为输入传递给查询的后半部分。

openCypher兼容性

本文操作仅适用于openCypher方式。

说明:在nGQL扩展中,有与WITH类似的管道符,但它们的工作方式不同。不要在openCypher方式中使用管道符,也不要在nGQL扩展中使用WITH子句。

组成复合查询

使用WITH子句可以组合语句,将一条语句的输出转换为另一条语句的输入。

示例1

  1. 匹配一个路径。
  2. 通过nodes()函数将路径上的所有点输出到一个列表。
  3. 将列表拆分为行。
  4. 去重后返回点的信息。
  1. nebula> MATCH p=(v:player{name:"Tim Duncan"})--() \
  2. WITH nodes(p) AS n \
  3. UNWIND n AS n1 \
  4. RETURN DISTINCT n1;
  5. +----------------------------------------------------------------------+
  6. | n1 |
  7. +----------------------------------------------------------------------+
  8. | ("player100" :star{} :person{} :player{age: 42, name: "Tim Duncan"}) |
  9. +----------------------------------------------------------------------+
  10. | ("player101" :player{age: 36, name: "Tony Parker"}) |
  11. +----------------------------------------------------------------------+
  12. | ("team204" :team{name: "Spurs"}) |
  13. +----------------------------------------------------------------------+
  14. | ("player102" :player{age: 33, name: "LaMarcus Aldridge"}) |
  15. +----------------------------------------------------------------------+
  16. | ("player125" :player{age: 41, name: "Manu Ginobili"}) |
  17. +----------------------------------------------------------------------+
  18. | ("player104" :player{age: 32, name: "Marco Belinelli"}) |
  19. +----------------------------------------------------------------------+
  20. | ("player144" :player{age: 47, name: "Shaquile O'Neal"}) |
  21. +----------------------------------------------------------------------+
  22. | ("player105" :player{age: 31, name: "Danny Green"}) |
  23. +----------------------------------------------------------------------+
  24. | ("player113" :player{age: 29, name: "Dejounte Murray"}) |
  25. +----------------------------------------------------------------------+
  26. | ("player107" :player{age: 32, name: "Aron Baynes"}) |
  27. +----------------------------------------------------------------------+
  28. | ("player109" :player{age: 34, name: "Tiago Splitter"}) |
  29. +----------------------------------------------------------------------+
  30. | ("player108" :player{age: 36, name: "Boris Diaw"}) |
  31. +----------------------------------------------------------------------+

示例2

  1. 匹配点ID为player100的点。
  2. 通过labels()函数将点的所有标签输出到一个列表。
  3. 将列表拆分为行。
  4. 返回结果。
  1. nebula> MATCH (v) \
  2. WHERE id(v)=="player100" \
  3. WITH labels(v) AS tags_unf \
  4. UNWIND tags_unf AS tags_f \
  5. RETURN tags_f;
  6. +----------+
  7. | tags_f |
  8. +----------+
  9. | "star" |
  10. +----------+
  11. | "player" |
  12. +----------+
  13. | "person" |
  14. +----------+

过滤聚合查询

WITH可以在聚合查询中作为过滤器使用。

  1. nebula> MATCH (v:player)-->(v2:player) \
  2. WITH DISTINCT v2 AS v2, v2.age AS Age \
  3. ORDER BY Age \
  4. WHERE Age<25 \
  5. RETURN v2.name AS Name, Age;
  6. +----------------------+-----+
  7. | Name | Age |
  8. +----------------------+-----+
  9. | "Luka Doncic" | 20 |
  10. +----------------------+-----+
  11. | "Ben Simmons" | 22 |
  12. +----------------------+-----+
  13. | "Kristaps Porzingis" | 23 |
  14. +----------------------+-----+

collect()之前处理输出

collect()函数将输出结果转换为列表之前,可以使用WITH子句排序和限制输出结果。

  1. nebula> MATCH (v:player) \
  2. WITH v.name AS Name \
  3. ORDER BY Name DESC \
  4. LIMIT 3 \
  5. RETURN collect(Name);
  6. +-----------------------------------------------+
  7. | COLLECT(Name) |
  8. +-----------------------------------------------+
  9. | ["Yao Ming", "Vince Carter", "Tracy McGrady"] |
  10. +-----------------------------------------------+

结合RETURN语句使用

WITH子句中设置别名,并通过RETURN子句输出结果。

  1. nebula> WITH [1, 2, 3] AS list RETURN 3 IN list AS r;
  2. +------+
  3. | r |
  4. +------+
  5. | true |
  6. +------+
  7. nebula> WITH 4 AS one, 3 AS two RETURN one > two AS result;
  8. +--------+
  9. | result |
  10. +--------+
  11. | true |
  12. +--------+