RETURN

RETURN子句定义了nGQL查询的输出结果。如果需要返回多个字段,用英文逗号(,)分隔。

RETURN可以引导子句或语句:

  • RETURN子句可以用于nGQL中的openCypher方式语句中,例如MATCHUNWIND

  • RETURN可以单独使用,输出表达式的结果。

openCypher兼容性

本文操作仅适用于nGQL中的openCypher方式。关于原生nGQL如何定义输出结果,请参见YIELD

RETURN不支持如下openCypher功能:

  • 使用不在英文字母表中的字符作为变量名。例如:

    1. MATCH (`点1`:player) \
    2. RETURN `点1`;
  • 设置一个模式,并返回该模式匹配的所有元素。例如:

    1. MATCH (v:player) \
    2. RETURN (v)-[e]->(v2);

历史版本兼容性

  • 在nGQL 1.x中,RETURN适用于原生nGQL,语法为RETURN <var_ref> IF <var_ref> IS NOT NULL

  • 在nGQL 2.0中,RETURN不适用于原生nGQL。

Map顺序说明

RETURN返回Map时,Key的顺序是未定义的。

  1. nebula> RETURN {age: 32, name: "Marco Belinelli"};
  2. +------------------------------------+
  3. | {age:32,name:"Marco Belinelli"} |
  4. +------------------------------------+
  5. | {age: 32, name: "Marco Belinelli"} |
  6. +------------------------------------+
  7. nebula> RETURN {zage: 32, name: "Marco Belinelli"};
  8. +-------------------------------------+
  9. | {zage:32,name:"Marco Belinelli"} |
  10. +-------------------------------------+
  11. | {name: "Marco Belinelli", zage: 32} |
  12. +-------------------------------------+

返回点

  1. nebula> MATCH (v:player) \
  2. RETURN v;
  3. +---------------------------------------------------------------+
  4. | v |
  5. +---------------------------------------------------------------+
  6. | ("player104" :player{age: 32, name: "Marco Belinelli"}) |
  7. +---------------------------------------------------------------+
  8. | ("player107" :player{age: 32, name: "Aron Baynes"}) |
  9. +---------------------------------------------------------------+
  10. | ("player116" :player{age: 34, name: "LeBron James"}) |
  11. +---------------------------------------------------------------+
  12. | ("player120" :player{age: 29, name: "James Harden"}) |
  13. +---------------------------------------------------------------+
  14. | ("player125" :player{age: 41, name: "Manu Ginobili"}) |
  15. +---------------------------------------------------------------+
  16. ...

返回边

  1. nebula> MATCH (v:player)-[e]->() \
  2. RETURN e;
  3. +------------------------------------------------------------------------------+
  4. | e |
  5. +------------------------------------------------------------------------------+
  6. | [:follow "player104"->"player100" @0 {degree: 55}] |
  7. +------------------------------------------------------------------------------+
  8. | [:follow "player104"->"player101" @0 {degree: 50}] |
  9. +------------------------------------------------------------------------------+
  10. | [:follow "player104"->"player105" @0 {degree: 60}] |
  11. +------------------------------------------------------------------------------+
  12. | [:serve "player104"->"team200" @0 {end_year: 2009, start_year: 2007}] |
  13. +------------------------------------------------------------------------------+
  14. | [:serve "player104"->"team208" @0 {end_year: 2016, start_year: 2015}] |
  15. +------------------------------------------------------------------------------+
  16. ...

返回属性

使用语法{<vertex_name>|<edge_name>}.<property>返回点或边的属性。

  1. nebula> MATCH (v:player) \
  2. RETURN v.name, v.age \
  3. LIMIT 3;
  4. +-------------------+-------+
  5. | v.name | v.age |
  6. +-------------------+-------+
  7. | "Rajon Rondo" | 33 |
  8. +-------------------+-------+
  9. | "Rudy Gay" | 32 |
  10. +-------------------+-------+
  11. | "Dejounte Murray" | 29 |
  12. +-------------------+-------+

返回所有元素

使用星号(*)返回匹配模式中的所有元素。

  1. nebula> MATCH (v:player{name:"Tim Duncan"}) \
  2. RETURN *;
  3. +----------------------------------------------------+
  4. | v |
  5. +----------------------------------------------------+
  6. | ("player100" :player{age: 42, name: "Tim Duncan"}) |
  7. +----------------------------------------------------+
  8. nebula> MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
  9. RETURN *;
  10. +----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+
  11. | v | e | v2 |
  12. +----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+
  13. | ("player100" :player{age: 42, name: "Tim Duncan"}) | [:follow "player100"->"player101" @0 {degree: 95}] | ("player101" :player{age: 36, name: "Tony Parker"}) |
  14. +----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+
  15. | ("player100" :player{age: 42, name: "Tim Duncan"}) | [:follow "player100"->"player125" @0 {degree: 95}] | ("player125" :player{age: 41, name: "Manu Ginobili"}) |
  16. +----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+
  17. | ("player100" :player{age: 42, name: "Tim Duncan"}) | [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] | ("team204" :team{name: "Spurs"}) |
  18. +----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+

重命名字段

使用语法AS <alias>重命名输出结果中的字段。

  1. nebula> MATCH (v:player{name:"Tim Duncan"})-[:serve]->(v2) \
  2. RETURN v2.name AS Team;
  3. +---------+
  4. | Team |
  5. +---------+
  6. | "Spurs" |
  7. +---------+
  8. nebula> RETURN "Amber" AS Name;
  9. +---------+
  10. | Name |
  11. +---------+
  12. | "Amber" |
  13. +---------+

返回不存在的属性

如果匹配的结果中,某个属性不存在,会返回NULL

  1. nebula> MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
  2. RETURN v2.name, type(e), v2.age;
  3. +-----------------+----------+----------+
  4. | v2.name | type(e) | v2.age |
  5. +-----------------+----------+----------+
  6. | "Tony Parker" | "follow" | 36 |
  7. +-----------------+----------+----------+
  8. | "Manu Ginobili" | "follow" | 41 |
  9. +-----------------+----------+----------+
  10. | "Spurs" | "serve" | __NULL__ |
  11. +-----------------+----------+----------+

返回表达式结果

RETURN语句可以返回字面量、函数或谓词等表达式的结果。

  1. nebula> MATCH (v:player{name:"Tony Parker"})-->(v2:player) \
  2. RETURN DISTINCT v2.name, "Hello"+" graphs!", v2.age > 35;
  3. +---------------------+------------------+-------------+
  4. | v2.name | (Hello+ graphs!) | (v2.age>35) |
  5. +---------------------+------------------+-------------+
  6. | "Tim Duncan" | "Hello graphs!" | true |
  7. +---------------------+------------------+-------------+
  8. | "LaMarcus Aldridge" | "Hello graphs!" | false |
  9. +---------------------+------------------+-------------+
  10. | "Manu Ginobili" | "Hello graphs!" | true |
  11. +---------------------+------------------+-------------+
  12. nebula> RETURN 1+1;
  13. +-------+
  14. | (1+1) |
  15. +-------+
  16. | 2 |
  17. +-------+
  18. nebula> RETURN 3 > 1;
  19. +-------+
  20. | (3>1) |
  21. +-------+
  22. | true |
  23. +-------+
  24. RETURN 1+1, rand32(1, 5);
  25. +-------+-------------+
  26. | (1+1) | rand32(1,5) |
  27. +-------+-------------+
  28. | 2 | 1 |
  29. +-------+-------------+

返回唯一字段

使用DISTINCT可以删除结果集中的重复字段。

  1. # 未使用DISTINCT。
  2. nebula> MATCH (v:player{name:"Tony Parker"})--(v2:player) \
  3. RETURN v2.name, v2.age;
  4. +---------------------+--------+
  5. | v2.name | v2.age |
  6. +---------------------+--------+
  7. | "Tim Duncan" | 42 |
  8. +---------------------+--------+
  9. | "LaMarcus Aldridge" | 33 |
  10. +---------------------+--------+
  11. | "Marco Belinelli" | 32 |
  12. +---------------------+--------+
  13. | "Boris Diaw" | 36 |
  14. +---------------------+--------+
  15. | "Dejounte Murray" | 29 |
  16. +---------------------+--------+
  17. | "Tim Duncan" | 42 |
  18. +---------------------+--------+
  19. | "LaMarcus Aldridge" | 33 |
  20. +---------------------+--------+
  21. | "Manu Ginobili" | 41 |
  22. +---------------------+--------+
  23. # 使用DISTINCT。
  24. nebula> MATCH (v:player{name:"Tony Parker"})--(v2:player) \
  25. RETURN DISTINCT v2.name, v2.age;
  26. +---------------------+--------+
  27. | v2.name | v2.age |
  28. +---------------------+--------+
  29. | "Tim Duncan" | 42 |
  30. +---------------------+--------+
  31. | "LaMarcus Aldridge" | 33 |
  32. +---------------------+--------+
  33. | "Marco Belinelli" | 32 |
  34. +---------------------+--------+
  35. | "Boris Diaw" | 36 |
  36. +---------------------+--------+
  37. | "Dejounte Murray" | 29 |
  38. +---------------------+--------+
  39. | "Manu Ginobili" | 41 |
  40. +---------------------+--------+

最后更新: August 25, 2021