跳到主要内容
版本:3.11.0

YIELD

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

YIELD可以引导子句或语句:

  • YIELD子句用于原生 nGQL 语句中,例如GOFETCHLOOKUP,必须通过YIELD子句定义返回结果。

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

openCypher 兼容性

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

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

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

    备注

    nGQL 不支持CALL[…YIELD]

:::

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

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

YIELD 子句

语法

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

使用 YIELD 子句

  • GO语句中使用YIELD

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

    nebula> FETCH PROP ON player "player100" \
    YIELD properties(vertex).name;
    +-------------------------+
    | properties(VERTEX).name |
    +-------------------------+
    | "Tim Duncan" |
    +-------------------------+
  • LOOKUP语句中使用YIELD

    nebula> LOOKUP ON player WHERE player.name == "Tony Parker" \
    YIELD properties(vertex).name, properties(vertex).age;
    +-------------------------+------------------------+
    | properties(VERTEX).name | properties(VERTEX).age |
    +-------------------------+------------------------+
    | "Tony Parker" | 36 |
    +-------------------------+------------------------+

YIELD 语句

语法

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

复合查询中使用 YIELD 语句

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

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

# 查找 player 中年龄大于 30 且小于 32 的点,返回去掉重复属性的值。
nebula> LOOKUP ON player \
WHERE player.age < 32 and player.age >30 \
YIELD DISTINCT properties(vertex).age as v;
+--------+
| v |
+--------+
| 31 |
+--------+

独立使用 YIELD 语句

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

nebula> YIELD rand32(1, 6);
+-------------+
| rand32(1,6) |
+-------------+
| 3 |
+-------------+

nebula> YIELD "Hel" + "\tlo" AS string1, ", World!" AS string2;
+-------------+------------+
| string1 | string2 |
+-------------+------------+
| "Hel lo" | ", World!" |
+-------------+------------+

nebula> YIELD hash("Tim") % 100;
+-------------------+
| (hash("Tim")%100) |
+-------------------+
| 42 |
+-------------------+

nebula> YIELD \
CASE 2+3 \
WHEN 4 THEN 0 \
WHEN 5 THEN 1 \
ELSE -1 \
END \
AS result;
+--------+
| result |
+--------+
| 1 |
+--------+

nebula> YIELD 1- -1;
+----------+
| (1--(1)) |
+----------+
| 2 |
+----------+