• 集合操作 (UNIONINTERSECTMINUS)
    • UNION,UNION DISTINCT,UNION ALL
      • 示例
    • INTERSECT
    • MINUS

    集合操作 (UNIONINTERSECTMINUS)

    UNION,UNION DISTINCT,UNION ALL

    UNION DISTINCT (简称 UNION)返回数据集 A 和 B 的并集(不包含重复元素)。

    UNION ALL 返回数据集 A 和 B 的并集(包含重复元素)。UNION 语法为

    1. <left> UNION [DISTINCT | ALL] <right>

    <left><right> 必须列数相同,且数据类型相同。

    示例

    1. nebula> GO FROM 1 OVER e1 \
    2. UNION \
    3. GO FROM 2 OVER e1

    以上语句返回点 12 (沿边 e1) 关联的唯一的点。

    1. nebula> GO FROM 1 OVER e1 \
    2. UNION ALL\
    3. GO FROM 2 OVER e1

    以上语句返回点 12 关联的点,其中存在重复点。

    UNION 亦可与 YIELD 同时使用,例如以下语句:

    1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS left_1, $$.tag.prop2 AS left_2 -- query 1
    2. ==========================
    3. | id | left_1 | left_2 |
    4. ==========================
    5. | 104 | 1 | 2 | -- line 1
    6. --------------------------
    7. | 215 | 4 | 3 | -- line 3
    8. --------------------------
    9. nebula> GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS right_1, $$.tag.prop2 AS right_2 -- query 2
    10. ===========================
    11. | id | right_1 | right_2 |
    12. ===========================
    13. | 104 | 1 | 2 | -- line 1
    14. ---------------------------
    15. | 104 | 2 | 2 | -- line 2
    16. ---------------------------
    1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS left_1, $$.tag.prop2 AS left_2
    2. UNION /* DISTINCT */
    3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS right_1, $$.tag.prop2 AS right_2

    以上语句返回

    1. =========================
    2. | id | left_1 | left_2 | -- UNION or UNION DISTINCT. The column names come from query 1
    3. =========================
    4. | 104 | 1 | 2 | -- line 1
    5. -------------------------
    6. | 104 | 2 | 2 | -- line 2
    7. -------------------------
    8. | 215 | 4 | 3 | -- line 3
    9. -------------------------

    请注意第一行与第二行返回相同 id 的点,但是返回的值不同。DISTINCT 检查返回结果中的重复值。所以第一行与第二行的返回结果不同。UNION ALL 返回结果为

    1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS left_1, $$.tag.prop2 AS left_2 \
    2. UNION ALL \
    3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS right_1, $$.tag.prop2 AS right_2
    4. =========================
    5. | id | left_1 | left_2 | -- UNION ALL
    6. =========================
    7. | 104 | 1 | 2 | -- line 1
    8. -------------------------
    9. | 104 | 1 | 2 | -- line 1
    10. -------------------------
    11. | 104 | 2 | 2 | -- line 2
    12. -------------------------
    13. | 215 | 4 | 3 | -- line 3
    14. -------------------------

    INTERSECT

    INTERSECT 返回集合 A 和 B ( A ⋂ B)的交集。

    1. <left> INTERSECT <right>

    UNION 类似, <left><right> 必须列数相同,且数据类型相同。此外,只返回 <left><right> 相同的行。例如:

    1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS left_1, $$.tag.prop2 AS left_2
    2. INTERSECT
    3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS right_1, $$.tag.prop2 AS right_2

    返回

    1. =========================
    2. | id | left_1 | left_2 |
    3. =========================
    4. | 104 | 1 | 2 | -- line 1
    5. -------------------------

    MINUS

    返回 A - B 数据的差集,此处请注意运算顺序。例如:

    1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS left_1, $$.tag.prop2 AS left_2
    2. MINUS
    3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS right_1, $$.tag.prop2 AS right_2

    返回

    1. ==========================
    2. | id | left_1 | left_2 |
    3. ==========================
    4. | 215 | 4 | 3 | -- line 3
    5. --------------------------

    如果更改 MINUS 顺序

    1. nebula> GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS right_1, $$.tag.prop2 AS right_2
    2. MINUS
    3. GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS left_1, $$.tag.prop2 AS left_2

    则返回

    1. ===========================
    2. | id | right_1 | right_2 | -- column named from query 2
    3. ===========================
    4. | 104 | 2 | 2 | -- line 2
    5. ---------------------------