YIELD

YIELD定义nGQL查询的输出结果。

YIELD可以引导子句或语句:

  • YIELD子句可以用于原生nGQL语句中,例如GOFETCHLOOKUP

  • YIELD语句可以在独立查询或复合查询中使用。

openCypher兼容性

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

YIELD在nGQL和openCypher中有不同的函数:

  • 在openCypher中,YIELD用于在CALL[…YIELD]子句中指定过程调用的输出。

    Note

    nGQL不支持CALL[…YIELD]

  • 在nGQL中,YIELD和openCypher中的RETURN类似。

Note

下文示例中的$$$-等是引用符号,详情请参见引用符

YIELD子句

语法

  1. YIELD [DISTINCT] <col> [AS <alias>] [, <col> [AS <alias>] ...];
参数说明
DISTINCT聚合输出结果,返回去重后的结果集。
col要返回的字段。如果没有为字段设置别名,返回结果中的列名为col
aliascol的别名。使用关键字AS进行设置,设置后返回结果中的列名为该别名。

使用YIELD子句

  • GO语句中使用YIELD

    1. nebula> GO FROM "player100" OVER follow \
    2. YIELD properties($$).name AS Friend, properties($$).age AS Age;
    3. +-----------------+-----+
    4. | Friend | Age |
    5. +-----------------+-----+
    6. | "Tony Parker" | 36 |
    7. | "Manu Ginobili" | 41 |
    8. +-----------------+-----+
  • FETCH语句中使用YIELD

    1. nebula> FETCH PROP ON player "player100" \
    2. YIELD properties(vertex).name;
    3. +-------------+-------------------------+
    4. | VertexID | properties(VERTEX).name |
    5. +-------------+-------------------------+
    6. | "player100" | "Tim Duncan" |
    7. +-------------+-------------------------+
  • LOOKUP语句中使用YIELD

    1. nebula> LOOKUP ON player WHERE player.name == "Tony Parker" \
    2. YIELD properties(vertex).name, properties(vertex).age;
    3. +-------------+-------------------------+------------------------+
    4. | VertexID | properties(VERTEX).name | properties(VERTEX).age |
    5. +-------------+-------------------------+------------------------+
    6. | "player101" | "Tony Parker" | 36 |
    7. +-------------+-------------------------+------------------------+

YIELD语句

语法

  1. YIELD [DISTINCT] <col> [AS <alias>] [, <col> [AS <alias>] ...]
  2. [WHERE <conditions>];
参数说明
DISTINCT聚合输出结果,返回去重后的结果集。
col要按返回的字段。如果没有为字段设置别名,返回结果中的列名为col
aliascol的别名。使用关键字AS进行设置,设置后返回结果中的列名为该别名。
conditionsWHERE子句中设置的过滤条件。详情请参见WHERE

复合查询中使用YIELD语句

复合查询中,YIELD语句可以接收、过滤、修改之前语句的结果集,然后输出。

  1. # 查找player100关注的player,并计算他们的平均年龄。
  2. nebula> GO FROM "player100" OVER follow \
  3. YIELD dst(edge) AS ID \
  4. | FETCH PROP ON player $-.ID \
  5. YIELD properties(vertex).age AS Age \
  6. | YIELD AVG($-.Age) as Avg_age, count(*)as Num_friends;
  7. +---------+-------------+
  8. | Avg_age | Num_friends |
  9. +---------+-------------+
  10. | 38.5 | 2 |
  11. +---------+-------------+
  1. # 查找player101关注的player,返回degree大于90的player。
  2. nebula> $var1 = GO FROM "player101" OVER follow \
  3. YIELD properties(edge).degree AS Degree, dst(edge) as ID; \
  4. YIELD $var1.ID AS ID WHERE $var1.Degree > 90;
  5. +-------------+
  6. | ID |
  7. +-------------+
  8. | "player100" |
  9. | "player125" |
  10. +-------------+

独立使用YIELD语句

YIELD可以计算表达式并返回结果。

  1. nebula> YIELD rand32(1, 6);
  2. +-------------+
  3. | rand32(1,6) |
  4. +-------------+
  5. | 3 |
  6. +-------------+
  7. nebula> YIELD "Hel" + "\tlo" AS string1, ", World!" AS string2;
  8. +-------------+------------+
  9. | string1 | string2 |
  10. +-------------+------------+
  11. | "Hel lo" | ", World!" |
  12. +-------------+------------+
  13. nebula> YIELD hash("Tim") % 100;
  14. +-----------------+
  15. | (hash(Tim)%100) |
  16. +-----------------+
  17. | 42 |
  18. +-----------------+
  19. nebula> YIELD \
  20. CASE 2+3 \
  21. WHEN 4 THEN 0 \
  22. WHEN 5 THEN 1 \
  23. ELSE -1 \
  24. END \
  25. AS result;
  26. +--------+
  27. | result |
  28. +--------+
  29. | 1 |
  30. +--------+

最后更新: October 27, 2021